The problem is that the $product
object does not exist where you're trying to access it (when you're hitting "publish" in the editor).
As Jacob Peattie mentioned in the comments, you could probably get it all from the $post
object. But, you can get the $product
object from the $post->ID
and then use the "get_" methods available in the object to get the values you need.
Here's your revised code for getting these values via $product
:
function sync_on_product_save( $new_status, $old_status, $post ) {
global $post;
global $product;
if (
$old_status != 'publish'
&& $new_status == 'publish'
&& ! empty( $post->ID )
&& in_array( $post->post_type, array( 'product' ) )
) {
// Get the product object for this ID:
$product = wc_get_product( $post->ID );
// Use "get_" methods for values:
$product_sku = $product->get_sku();
$product_name = $product->get_name();
$product_price = $product->get_price();
$product_desc = $product->get_description();
// Do what you need for 3rd party here...
}
}
add_action( 'transition_post_status', 'sync_on_product_save', 10, 3 );