WooCommerce 保存后获取产品类型

WooCommerce get product type after save

你能告诉我如何在产品保存后获取产品类型吗?

add_action( 'save_post_product', 'nd_update_group_product_attributes_func', 1001, 3 );   

function nd_update_group_product_attributes_func($post_ID, $post, $update)
{

    if($post->post_type == 'product'){

        $childrens   = $post->get_children();
        $product_type = $post->get_type();  /* this will return simple for all product type*/ 


    }

}

连我都有“分组”的产品类型。 $product_type = $post->get_type();在 save_post_product 挂钩上 return 我们第一次创建产品时的“简单”,稍后更新将 return “分组”。

if( $_product->is_type( 'simple' ) ) $产品->is_type( 'variable' )

使用函数 wc_get_product

获取 WooCommerce 产品对象
add_action('woocommerce_after_product_object_save', 'nd_update_group_product_attributes_func', 10, 2);

function nd_update_group_product_attributes_func($product, $data_store) {

    $product_type = $product->get_type();
    if ($product->is_type('grouped')) {

        $child_product_ids = $product->get_children();
        $all_child_bed_attributes = array();

        foreach ($child_product_ids as $child_product_id) {

            $child_product = wc_get_product($child_product_id);
            $pa_bedrooms = $child_product->get_attribute('pa_bedrooms');
            $all_child_bed_attributes = array_unique(array_merge($all_child_bed_attributes, $pa_bedrooms));
        }
        if ($all_child_bed_attributes) {

            foreach ($all_child_bed_attributes as $bed) {
                wp_set_object_terms($post_ID, $bed, 'pa_bedrooms', true);
            }
        }
    }
}

这里可以使用挂钩woocommerce_after_product_object_save