woocommerce hook publish product

I want to add an action whenever the admin publishes a product, but the WP hook publish_post does not trigger then – even though a wc product is just another type of post.

I haven’t found a woocommerce hook that triggers when a product is published.

Read More

Any thoughts?

Related posts

3 comments

  1. I recommand you to use the transition_post_status. See example below :

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

    This hook is really handy. It allows you to target a specific action: every time post data is saved. But with code I add you can avoid trigger your code if it’s a draft save or an updtate.

  2. If You add a new product in woocommerce then send product_id

    add_action( 'draft_to_publish', 'my_product_update' );
    function my_product_update( $post ) {
        if ( $post->post_type == "product" ) {
             $productId = $post->ID;
             // your code
        }
    }
    

Comments are closed.