I am able to successfully pull and update post meta as expected using the following.
function update_post_type_post_prodd( $post_id ) {
$personalization = get_post_meta( $post_id, '_qty_discount_code', true );
if ($personalization === 'R'){
$gfdata ='a:28:{s:2:"id";}';
$emptyRemoved = unserialize($gfdata);
update_post_meta( $post_id,'_gravity_form_data', $emptyRemoved );
}
}
add_action( 'save_post_product', 'update_post_type_post_prodd' );
Pull meta field _qty_discount_code
and if it contains the value R
update the meta field _gravity_form_data
with $gfdata
on post save.
However, when attempting to pull a product attribute taxonomy pa_personalization
I get crickets.
I have tried multiple methods to no avail. Does this looks correct?
function update_post_type_post_prodd( $post_id ) {
//$personalization = get_post_meta( $post_id, 'pa_personalization', true );
$taxonomy = 'pa_personalization';
$meta = get_post_meta($post_id, 'attribute_'.$taxonomy, true);
$term = get_term_by('slug', $meta, $taxonomy);
$personalization = $term->name;
if ($personalization === 'yes'){
$emptyRemoved = unserialize($gfdata);
update_post_meta( $post_id,'_gravity_form_data', $emptyRemoved );
}
}
//execute the function whenever post type is being updated
add_action( 'save_post_product', 'update_post_type_post_prodd' );
CodePudding user response:
add_action('save_post_product', 'update_post_type_post_prodd', 10, 3);
function update_post_type_post_prodd($post_id, $post, $update) {
$product = wc_get_product($post_id);
$pa_personalization = $product->get_attribute('pa_personalization');
$attributes_values = explode(',', $pa_personalization);
if (in_array('yes', $attributes_values)) {
// Do stuff here
}
}
The hook do_action( "save_post_{$post->post_type}", int $post_ID, WP_Post $post, bool $update )
receives 3 parameter.