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.

Any thoughts?

3 Answers
3

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.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *