WooCommerce 订单状态完成时更新产品库存数量和库存状态
我们销售手工制品。可变产品只能购买一种变体。
订购变体后,该变体产品的库存将变为 0,并且在订单完成之前无法订购该产品。我可以按如下方式设置库存状态和库存数量:
//when order is completed the stock and stock status are reset to instock and 1
add_action( 'woocommerce_order_status_completed', 'my_function' );
function my_function($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
$product_id;
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
} //end foreach
$new_stock_status = 'instock';
// 1. Updating the stock quantity
update_post_meta($product_id, '_stock', 1);
// 2. Updating the stock quantity
update_post_meta( $product_id, '_stock_status', wc_clean( $new_stock_status ) );
// 3. Updating post term relationship
wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );
// And finally (optionally if needed)
wc_delete_product_transients( $product_id ); // Clear/refresh the variation cache
}
我不确定此代码是否正确,但它似乎有效。
但单品页面依然显示缺货信息。仅当我添加:
//reset stock in product
add_action( 'woocommerce_admin_process_product_object', 'lumi_reset_stock' );
function lumi_reset_stock( $product ) {
// Using WC setters
$product->set_manage_stock(true);
$product->set_stock_quantity(1);
$product->set_stock_status('instock');
}
并将产品保存在管理后端时,才能再次购买该产品。
这有点复杂。订单完成后,如何重置产品的库存和 stock_status?
查看数据库> wp_wc_product_meta_lookup 即使保存产品后,库存状态仍显示缺货且数量为 0。我必须重置这些值吗?
We sell handmade products. Only one variation of a variable product is purchasable.
When a variation is ordered, the stock of that variable product goes to 0 and the product cannot be ordered until the order is completed. I can set the instock status and the stock quantity as follows:
//when order is completed the stock and stock status are reset to instock and 1
add_action( 'woocommerce_order_status_completed', 'my_function' );
function my_function($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
$product_id;
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
} //end foreach
$new_stock_status = 'instock';
// 1. Updating the stock quantity
update_post_meta($product_id, '_stock', 1);
// 2. Updating the stock quantity
update_post_meta( $product_id, '_stock_status', wc_clean( $new_stock_status ) );
// 3. Updating post term relationship
wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );
// And finally (optionally if needed)
wc_delete_product_transients( $product_id ); // Clear/refresh the variation cache
}
I am not sure whether this code is correct, but it seems to be working.
However, the single product page persists in showing the out of stock message. Only when I add:
//reset stock in product
add_action( 'woocommerce_admin_process_product_object', 'lumi_reset_stock' );
function lumi_reset_stock( $product ) {
// Using WC setters
$product->set_manage_stock(true);
$product->set_stock_quantity(1);
$product->set_stock_status('instock');
}
And save the product in the admin backend, then the product is purchasable again.
This is a bit complicated. How can i also reset the stock and stock_status for a product when an order is completed?
Looking at the database > wp_wc_product_meta_lookup the stock-status shows outofstock and the quantity is 0, even after saving the product. Do i have to reset these values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管您的代码有效,但您缺少一些检查/点:
所以你得到:
Although your code works, you are missing some checks/points:
So you get: