Wordpress/WooCommerce hooks not firing?

CodeWarrior

I've been through a lot of blogs and posts on SO before asking this question here.

I'm using WooCommerce plugin and was trying to get product data via action hooks to be passed to a 3rd party API.

I've tried various blocks of code but none of them gives me product information.

So, I'm trying to add/update/delete a product on WooCommerce with the following hooks placed in my child theme's functions.php file.

Hook 1

function on_all_status_transitions( $new_status, $old_status, $post ) {
    echo '<script>console.log("old_status: ' . $old_status . '")</script>';
    echo '<script>console.log("new_status: ' . $new_status . '")</script>';
}
add_action('transition_post_status', 'on_all_status_transitions', 10, 3 );

This only prints once when $new_status is auto-draft and then when I save it as a draft or publish or move it to trash, nothing happens.

Hook 2

function sync_on_product_save($product_Id){
    $product = wc_get_product($product_Id);
    echo '<script>console.log("Product Id: ' . $product_Id . '")</script>';    
    // Do what you need for 3rd party here...
}
add_action('woocommerce_new_product', 'sync_on_product_save', 10, 1);

This hook never gets triggered.

Hook 3

Same happens with the product update hook, never triggered.

add_action('woocommerce_update_product', 'sync_on_product_save', 10, 1);

With all the reading that I have done, it seems that it is very obvious and should just work. So if someone can just point out what I might be doing wrong or something that I might be missing it would be great.

Also, I am aware that we can create webhooks in WooCommerce that can deliver the payload to a URL that we can specify. I would like to do this as a last option.

Any help is highly appreciated. Thanks in advance.

Note: I'm running my Wordpress instance on localhost over wamp with https enabled.

Sushil Adhikari

Your hooks were firing all the time, while you are updating the products. You could see product Id when you dump the product id and kill the PHP execution.

function sync_on_product_save($product_Id){
    var_dump( $product_Id ); exit;
    echo '<script>console.log("Product Id: ' . $product_Id . '")</script>';    
    // Do what you need for 3rd party here...
}
add_action('woocommerce_update_product', 'sync_on_product_save', 10, 1);

The process will be like this: You hit the update product button, it will take you to post.php where your scripts will be added, soon it will redirect to your original product edit URL[http://example.com/wp-admin/post.php?post=xx&action=edit]. This will remove your added scripts, which is why you are not able to see the scripts.

Thanks

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related