Woocommerce Product publish, update and delete hooks

16,517

Solution 1

Woocommerce Products are basically wordpress posts. You can use wordpress hooks

add_action( 'before_delete_post', 'wpse_110037_new_posts' );
add_action( 'save_post', 'wpse_110037_new_posts' );

function wpse_110037_new_posts($post_id){
    $WC_Product = wc_get_product( $post_id);
}

wc_get_product() will return WC_Product object and you can get the product details from it.

Solution 2

I Prefer to check if the status if not a draft. Also you can have the third parameter updateto check if it's an update or not

add_action( 'save_post', array($this, 'wpse1511_create_or_update_product' ), 10, 3);

function wpse1511_create_or_update_product($post_id, $post, $update){
    if ($post->post_status != 'publish' || $post->post_type != 'product') {
        return;
    }

    if (!$product = wc_get_product( $post )) {
        return;
    }

    // Make something with $product
    // You can also check $update
}
Share:
16,517
Ketan
Author by

Ketan

I am a Top rated expert developer with 5+ years of experience in Wordpress and Woocommerce and 4+ year of experience in overall web development.

Updated on June 04, 2022

Comments

  • Ketan
    Ketan almost 2 years

    I need Woocommerce Product publish, update and delete hooks if any one know then please inform me.

    I find this hook :

    add_action('transition_post_status', 'wpse_110037_new_posts', 10, 3);
     function wpse_110037_new_posts($new_status, $old_status, $post) {
     if( 
            $old_status != 'publish' 
            && $new_status == 'publish' 
            && !empty($post->ID) 
            && in_array( $post->post_type, 
                array( 'product') 
                )
            ) {
              //add some cde here
         }
    
      }
    

    but it's only display product id, title, publish status etc....but i want product price, category, tag, brand and stock status.

    So please replay me if any one know.

    Thanks, Ketan.