skip to Main Content

Can you please tell me how can I get product type after product 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*/ 


    }

}

Even I have "grouped" product type.
$product_type = $post->get_type(); on save_post_product hook will return the "simple" when we first create the product and later on update will return "grouped".

2

Answers


  1. if( $_product->is_type( ‘simple’ ) )
    $product->is_type( ‘variable’ )

    Login or Signup to reply.
  2. Get the WooCommerce product object using the function wc_get_product

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

    The hook woocommerce_after_product_object_save can be used here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search