WooCommerce 获取产品属性分类值

WooCommerce get product attribute taxonomy value

我能够使用以下方法成功提取和更新 post 元数据。

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' );

拉取元字段_qty_discount_code

如果它包含值 R

在 post 保存时用 $gfdata 更新元字段 _gravity_form_data

但是,在尝试提取产品属性分类时pa_personalization我得到了蟋蟀。

我试了很多方法都没有用。这看起来正确吗?

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' );
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
    }
}

钩子do_action( "save_post_{$post->post_type}", int $post_ID, WP_Post $post, bool $update )接收3个参数。