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.