[英]Update product stock quantity and status in WooCommerce 3
I'm trying to have the product's stock quantity set to zero when there is an specific post_meta当有特定的 post_meta 时,我试图将产品的库存数量设置为零
I'm hooking into 'woocommerce_update_product'.我正在连接到“woocommerce_update_product”。 When I click "update", this starts, and does update correctly;
当我单击“更新”时,它开始并正确更新; however, the action never finishes.
然而,行动永远不会结束。 ( the page is just loading... )
(页面正在加载...)
When I refresh the page and check the stock, this is modified correctly.当我刷新页面并检查库存时,这是正确修改的。
Am I doing anything wrong?我做错了什么吗?
This is my code这是我的代码
add_action('woocommerce_update_product', 'sv_set_no_stock_when_discontinued', 10, 1);
function sv_set_no_stock_when_discontinued($prodId){
$discontinued = get_post_meta($prodId, '_is_discontinued', true);
if($discontinued == 'yes'){
$product = wc_get_product($prodId);
// Using WC setters
$product->set_manage_stock(true);
$product->set_stock_quantity(0);
$product->set_stock_status('outofstock');
// Save Product
$product->save();
}
}
After leaving it for a while, i get the following errors:离开一段时间后,我收到以下错误:
Fatal error: Allowed memory size of 1077936128 bytes exhausted (tried to allocate 20480 bytes) in \wp-includes\meta.php on line 1078
Fatal error: Allowed memory size of 1077936128 bytes exhausted (tried to allocate 20480 bytes) in \wp-includes\meta.php 第 1078 行
Fatal error: Allowed memory size of 1077936128 bytes exhausted (tried to allocate 20480 bytes) in \wp-includes\class-wp-fatal-error-handler.php on line 72
Fatal error: Allowed memory size of 1077936128 bytes exhausted (tried to allocate 20480 bytes) in \wp-includes\class-wp-fatal-error-handler.php 第 72 行
The WC_Product Object is already included as 2nd argument for woocommerce_update_product
Hook… So you can change a little bit your code like: WC_Product Object 已经作为
woocommerce_update_product
Hook 的第二个参数包含在内……因此您可以稍微更改您的代码,例如:
But as this hook is located on WC_Product_Data_Store_CPT
Class, It doesn't really like the WC_Product
setter methods and especially save()
method.但是由于这个钩子位于
WC_Product_Data_Store_CPT
Class,它不太喜欢WC_Product
setter 方法,尤其是save()
方法。
So instead we will replace this hook with the following:因此,我们将用以下内容替换此钩子:
add_action( 'woocommerce_admin_process_product_object', 'set_no_stock_if_discontinued' );
function set_no_stock_if_discontinued( $product ) {
if( $product->get_meta('_is_discontinued') === 'yes'
|| ( isset($_POST['_is_discontinued'])
&& esc_attr($_POST['_is_discontinued']) === 'yes' ) ) {
// Using WC setters
$product->set_manage_stock(true);
$product->set_stock_quantity(0);
$product->set_stock_status('outofstock');
}
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的 functions.php 文件。 Tested and works.
测试和工作。
Note: The
save()
method is not needed as it's triggered just after this hook.注意:不需要
save()
方法,因为它是在这个钩子之后触发的。
Global note: You can also set "manage stock" to false.
全局注释:您还可以将“管理库存”设置为 false。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.