uap_wc_trigger_save_product_meta
Fires after saving product meta for WooCommerce, allowing custom actions to be performed.
add_action( 'uap_wc_trigger_save_product_meta', $callback, 10, 3 );
Description
Fires after WooCommerce product review metadata is saved. Developers can use this hook to perform custom actions or modify saved data based on the comment, matched recipe ID, additional arguments, and the product object. It's crucial for integrating custom logic with Uncanny Automator's product review triggers.
Usage
add_action( 'uap_wc_trigger_save_product_meta', 'your_function_name', 10, 3 );
Parameters
-
$comment(mixed) - This parameter contains the WordPress comment object, which includes details about the product review.
-
$matched_recipe_id(mixed) - This parameter contains the comment object, which holds all the data related to the product review.
-
$args(mixed) - This parameter holds the ID of the matched recipe that is currently being processed.
Examples
add_action( 'uap_wc_trigger_save_product_meta', 'my_uap_wc_save_product_meta', 10, 4 );
/**
* Processes and saves product-related meta data for Uncanny Automator WooCommerce triggers.
*
* This function is hooked into the 'uap_wc_trigger_save_product_meta' action, which is triggered
* when Uncanny Automator needs to save specific meta data related to a WooCommerce product
* for a trigger event. It extracts relevant information from the provided parameters and
* uses it to potentially add or update meta fields associated with the product.
*
* @param int $product_id The ID of the WooCommerce product.
* @param int $recipe_id The ID of the Uncanny Automator recipe.
* @param array $trigger_args An array of arguments passed to the trigger, containing data to be saved.
* @param string $object_type The type of object the meta data pertains to, typically 'product'.
*/
function my_uap_wc_save_product_meta( $product_id, $recipe_id, $trigger_args, $object_type ) {
// Ensure we are dealing with a product object type and valid IDs.
if ( 'product' !== $object_type || ! is_numeric( $product_id ) || ! is_numeric( $recipe_id ) || empty( $trigger_args ) ) {
return;
}
// Example: Save a custom meta field related to the trigger for this product.
// Let's assume $trigger_args might contain a 'customer_note' if the trigger
// was related to a new customer order and we want to log that note against the product.
if ( isset( $trigger_args['customer_note'] ) && ! empty( $trigger_args['customer_note'] ) ) {
$existing_notes = get_post_meta( $product_id, '_automator_customer_notes', true );
if ( empty( $existing_notes ) ) {
$existing_notes = array();
}
// Append the new note, ensuring uniqueness if needed (though this example doesn't handle it).
$existing_notes[] = array(
'recipe_id' => $recipe_id,
'note' => sanitize_textarea_field( $trigger_args['customer_note'] ),
'timestamp' => current_time( 'mysql' ),
);
update_post_meta( $product_id, '_automator_customer_notes', $existing_notes );
}
// Example: Update a meta field that tracks how many times a specific recipe has triggered for this product.
// This could be useful for analytics or limiting trigger frequency.
$trigger_count_meta_key = '_automator_recipe_trigger_count_' . $recipe_id;
$current_count = get_post_meta( $product_id, $trigger_count_meta_key, true );
if ( false === $current_count || '' === $current_count ) {
$current_count = 0;
}
$current_count++;
update_post_meta( $product_id, $trigger_count_meta_key, $current_count );
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-prodreview-approved.php:141
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-prodreview.php:137
uncanny-automator-pro/src/integrations/woocommerce/triggers/wc-prodreview-rating.php:168
public function comment_approved( $new_status, $old_status, $comment ) {
if ( 'review' !== (string) $comment->comment_type ) {
return;
}
if ( 'approved' !== (string) $new_status ) {
return;
}
if ( ! $comment instanceof WP_Comment ) {
return;
}
if ( isset( $comment->user_id ) && 0 === absint( $comment->user_id ) ) {
return;
}
$recipes = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
$required_post = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
$matched_recipe_ids = array();
//Add where option is set to Any post / specific post
foreach ( $recipes as $recipe_id => $recipe ) {
foreach ( $recipe['triggers'] as $trigger ) {
$trigger_id = $trigger['ID'];
if ( intval( '-1' ) === intval( $required_post[ $recipe_id ][ $trigger_id ] ) || absint( $required_post[ $recipe_id ][ $trigger_id ] ) === absint( $comment->comment_post_ID ) ) {
$matched_recipe_ids[] = array(
'recipe_id' => $recipe_id,
'trigger_id' => $trigger_id,
);
}
}
}
if ( empty( $matched_recipe_ids ) ) {
return;
}
// If recipe matches
foreach ( $matched_recipe_ids as $matched_recipe_id ) {
$pass_args = array(
'code' => $this->trigger_code,
'meta' => $this->trigger_meta,
'user_id' => $comment->user_id,
'recipe_to_match' => $matched_recipe_id['recipe_id'],
'trigger_to_match' => $matched_recipe_id['trigger_id'],
'post_id' => $comment->comment_post_ID,
);
$args = Automator()->maybe_add_trigger_entry( $pass_args, false );
do_action( 'uap_wp_comment_approve', $comment, $matched_recipe_id['recipe_id'], $matched_recipe_id['trigger_id'], $args );
do_action( 'uap_wc_trigger_save_product_meta', $comment->comment_post_ID, $matched_recipe_id['recipe_id'], $args, 'product' );
if ( $args ) {
foreach ( $args as $result ) {
if ( true === $result['result'] ) {
Automator()->maybe_trigger_complete( $result['args'] );
}
}
}
}
}