I want to fetch woocommerce product post data when we publish a new product. I’ve tried some hooks but it’s working only when we update the product.
Does anyone knows how can I get the product post data when adding new product?
Here is my code.
add_action( 'save_post_product', 'add_product_tocrm_through_woocommerce', 10, 3);
function add_product_tocrm_through_woocommerce($post_id, $post, $update){
if ( wp_is_post_revision( $post_id ) ) {
return;
}
if (!$product = wc_get_product( $post )) {
return;
}
$post_type = get_post_type( $post_id );
$post_status = get_post_status();
if ( $post_type == 'product' && $post_status == 'publish') {
$product = wc_get_product($post_id);
$pNameWoo = $product->name;
$price = $product->get_price();
$desc = $product->get_description();
$wooProduct[0]['name'] = $pNameWoo;
$wooProduct[0]['price'] = $price;
$wooProduct[0]['description'] = $desc;
save_woo_product_tocrm($wooProduct);
}
}
2
Answers
here is the solution using woocommerce_process_product_meta hook.
You’re using "save_post_product" hook, so you don’t need to check lots of things before triggering your CRM save function.
Also, you need to use "->get_name()" instead of "->name" and the long description cannot be gotten in the way you coded, but with $post->post_content instead: