2

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

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.

  • Any reason you're using in_array () with a single item rather than just ==? – RobinJ Jan 11 '17 at 12:29
  • @RobinJ yes, in fact you're right but I'd like to do things this way for maintenance purpose. If I want to add another post type to add it will be faster and I don't have to rewrite the line. Most of the time I use classes so in my code this looks like that in_array( $post->post_type, $this->allowed_post_types ) – JMau Jan 11 '17 at 15:11
  • Fair enough :-) – RobinJ Jan 11 '17 at 16:23
  • i was trying to implement this with some taxonomy terms, but i couldnt get this run, the terms where always empty on publish. i got it working on update (not with this specific function here, i used another one (but in the end i used this one)), but i wanted it to work on publish. i guess, because the terms are not saved, yet. glad i came up with the idea of using $_POST variable. its all in there.. – honk31 Nov 20 '18 at 20:13
1

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

I think the best way is to use :

add_action( 'publish_product', array($this, 'prd_published_message'), 10, 2);

function prd_published_message( $ID, $post ) {
  ...
}

http://woocommerce.wp-a2z.org/oik_api/wp_transition_post_status/

  • Could you post a link referencing this "publish_product" action hook? Because I'm not able to find it anywhere... If that would be a real WC hook, it would have a woocommerce_ prefix... – ClemC Aug 18 '17 at 13:39
  • Yes you are wright, it works but it's not documented – Damien JALLON Oct 4 '17 at 11:53
  • @ClemC It's too late but might be helpful to someone. it's {status}_{post_type} Hook codex.wordpress.org/Post_Status_Transitions – Jimish Gamit Apr 2 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.