Woocommerce Product publish, update and delete hooks
Asked Answered
G

4

8

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.

Granduncle answered 17/7, 2015 at 6:14 Comment(0)
C
18

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.

Cookbook answered 17/7, 2015 at 8:33 Comment(2)
Might pay to check whether it's a product otherwise it will run for all post types.Xavier
Instead of save_post and checking for the post_type, why not use the post_type save post hook? save_post_productElmira
J
8

The save_post and save_post_product hooks run before the post_meta is updated and since most of the WooCommerce product data is stored as post_meta, using them might cause issues.

Thankfully, since v3, there are specific WooCommerce hooks that run after a product is updated (woocommerce_update_product) and when a product is created (woocommerce_new_product).

add_action( 'woocommerce_new_product', 'on_product_save', 10, 1 );
add_action( 'woocommerce_update_product', 'on_product_save', 10, 1 );
function on_product_save( $product_id ) {
     $product = wc_get_product( $product_id );
     // do something with this product
}
Jerrold answered 13/7, 2022 at 9:14 Comment(3)
I tried this way, but it's not workingKerri
@Kerri of course it works, these action hooks are part of WC core. If you copied and pasted the above snippet as-is, then obviously it will do nothing.Jerrold
I don't think this hook works at all, as stated on this -> github.com/woocommerce/woocommerce/issues/23610Mccormac
E
7

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
}
Emissive answered 28/2, 2017 at 20:32 Comment(0)
R
0

This hook will run after WC has updated the product in the DB:

add_action('save_post_product', 'ns_sync_on_product_save', 10, 3);

function ns_sync_on_product_save( $post_id, $post, $update ) {
   $product = wc_get_product( $post_id );
}
Richburg answered 12/11, 2021 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.