在新的WooCommerce产品手动发布后如何调用功能
每当要手动创建产品时,我都在尝试创建额外的WooCommerce产品。
这就是我尝试的:
add_action('transition_post_status', 'on_product_creation', 10, 3);
function on_product_creation($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'publish'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
$post_data = array(
'post_name' => 'test2',
'post_title' => 'test2',
'post_status' => 'publish',
'post_type' => 'product',
);
$product_id = wp_insert_post( $post_data );
$product = new WC_Product_Subscription( $product_id );
$product->set_regular_price( 1 );
$product->set_price( 1 );
$product->save();
$id = $product->get_id();
update_post_meta($id, "_subscription_length", 12);
update_post_meta($id, "_subscription_price", 1);
$log = "productCreated";
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
}
}
由于某种原因,我的功能在创建产品后不会运行。
我只有在手动创建产品以避免递归(产品创建会触发本身)时才试图发射钩子 一种了解产品是如何创建的方法?
I am trying to create an extra WooCommerce product whenever a product is being created manually.
This is what i tried:
add_action('transition_post_status', 'on_product_creation', 10, 3);
function on_product_creation($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'publish'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
$post_data = array(
'post_name' => 'test2',
'post_title' => 'test2',
'post_status' => 'publish',
'post_type' => 'product',
);
$product_id = wp_insert_post( $post_data );
$product = new WC_Product_Subscription( $product_id );
$product->set_regular_price( 1 );
$product->set_price( 1 );
$product->save();
$id = $product->get_id();
update_post_meta($id, "_subscription_length", 12);
update_post_meta($id, "_subscription_price", 1);
$log = "productCreated";
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
}
}
For some reason my function doesn't run after I create a product.
I am trying to fire the hook only when the product been created manually to avoid recursion (a product creation will trigger itself) is there
a way to know how the product have been created ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此挂钩
woocommerce_process_product_meta
才能在手动创建/更新产品时运行。 WC 6.2 测试确定This hook
woocommerce_process_product_meta
will run only when the product is manually created/updated. Tested OK with WC 6.2