admin管理员组文章数量:1130349
My code is:
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')
)
) {
print_R($product);
}
}
add_action('transition_post_status', 'sync_on_product_save', 10, 3);
I want to pass the product price, sku, description, and name to third-party API.
Please help me.
My code is:
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')
)
) {
print_R($product);
}
}
add_action('transition_post_status', 'sync_on_product_save', 10, 3);
I want to pass the product price, sku, description, and name to third-party API.
Please help me.
Share Improve this question edited Nov 26, 2018 at 14:03 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 26, 2018 at 10:21 Rashmi GhatoleRashmi Ghatole 331 gold badge1 silver badge3 bronze badges 2 |1 Answer
Reset to default 5The 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 ) {
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 );
My code is:
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')
)
) {
print_R($product);
}
}
add_action('transition_post_status', 'sync_on_product_save', 10, 3);
I want to pass the product price, sku, description, and name to third-party API.
Please help me.
My code is:
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')
)
) {
print_R($product);
}
}
add_action('transition_post_status', 'sync_on_product_save', 10, 3);
I want to pass the product price, sku, description, and name to third-party API.
Please help me.
Share Improve this question edited Nov 26, 2018 at 14:03 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 26, 2018 at 10:21 Rashmi GhatoleRashmi Ghatole 331 gold badge1 silver badge3 bronze badges 2- What do you need it for? The 3rd argument of your function contains the post data. – Jacob Peattie Commented Nov 26, 2018 at 10:23
-
As Jacob mentioned, the
$postobject is passed via the action and through that you pretty much have access to what you need. However, to answer the question about the$productobject specifically, you need to give it a product ID (which is the post ID) to have it contain anything where you're trying to access it. See my answer below which provides a solution using the product object. – butlerblog Commented Nov 26, 2018 at 13:33
1 Answer
Reset to default 5The 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 ) {
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 );
本文标签: WooCommerceGlobal product is returning value null
版权声明:本文标题:WooCommerce - Global $product is returning value null 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749150952a2323807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$postobject is passed via the action and through that you pretty much have access to what you need. However, to answer the question about the$productobject specifically, you need to give it a product ID (which is the post ID) to have it contain anything where you're trying to access it. See my answer below which provides a solution using the product object. – butlerblog Commented Nov 26, 2018 at 13:33