根据 WooCommerce 中另一种产品的库存状态确定是否可以购买一种产品
我有一个 WooCommerce 商店,有 3 种不同的产品(ID 6241、6242 和 6243),并且我试图仅在前一个产品缺货时才可以购买该产品。
归结为:
- 虽然 ID 6241 有库存 > ID 6242 不可购买
- 当 ID 6241 缺货时 > 当ID 6242 有库存时,ID 6242 变得可购买
- > ID 6243 不可购买
- 当 ID 6242 缺货时 ID 6243 变为可购买
- 等等..
我成功地用 2 种不同的产品做到了这一点,但现在我正在尝试制作一个循环,以便我可以通过多个产品应用它(在最后,我应该有 6 种不同的产品)。
所以我写了一个循环来浏览我当前的 3 个产品:
add_filter('woocommerce_is_purchasable', '_is_stage_purchasable', 10, 2);
function _is_stage_purchasable($is_purchasable, $product) {
$product_id = array(6241,6242,6243); //array with product IDs
foreach($product_id as $id){ //loop through products
if( $product->id==$id && $product->get_stock_quantity() == 0 ){ //if product = product N and product N stock = 0
$value = $id + 1; //value = next product ID
$product = new WC_Product( $value );
return true; //set new product as purchasable
}else{ //if product = product N and product N stock > 0
$value = $id + 1; //value = next product ID
$product = new WC_Product( $value );
return false; //set new product as not purchasable
}
}
}
但我面临一些问题。如果 6241 缺货,使用此代码将使产品 6242 和 6243 无法购买。我不明白为什么?有人可以给我一些关于这个问题的提示吗?
I have an WooCommerce shop with 3 different products (IDs 6241, 6242 and 6243) and I'm trying to make a product purchasable only if the previous product is out of stock.
Which comes down to:
- While ID 6241 is in stock > ID 6242 is not purchasable
- When ID 6241 is out of stock > ID 6242 become purchasable
- While ID 6242 is in stock > ID 6243 is not purchasable
- When ID 6242 is out of stock ID 6243 become purchasable
- Et cetera..
I succeed to do that with 2 different products, but now I'm trying to make a loop so I can apply that through multiple products (at the end, I should have 6 different products).
So I write a loop to go through my 3 current products:
add_filter('woocommerce_is_purchasable', '_is_stage_purchasable', 10, 2);
function _is_stage_purchasable($is_purchasable, $product) {
$product_id = array(6241,6242,6243); //array with product IDs
foreach($product_id as $id){ //loop through products
if( $product->id==$id && $product->get_stock_quantity() == 0 ){ //if product = product N and product N stock = 0
$value = $id + 1; //value = next product ID
$product = new WC_Product( $value );
return true; //set new product as purchasable
}else{ //if product = product N and product N stock > 0
$value = $id + 1; //value = next product ID
$product = new WC_Product( $value );
return false; //set new product as not purchasable
}
}
}
But I'm facing some issues. Using this code makes the products 6242 and 6243 not purchasable if 6241 is out of stock. I don't understand why? Could someone give me some hints on that issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码包含一些错误:
$product->id
被替换为$product-> ;get_id(
) 自 WooCommerce 3.0+ 起$value = $id + 1
而它实际上是关于获取以前的产品对象,因此 -1 似乎是正确的方式get_stock_status()
您可以确定先前产品的状态,并据此使当前产品可购买,因此您得到:
Your code contains some mistakes:
$product->id
is replaced by$product->get_id(
) since WooCommerce 3.0+$value = $id + 1
while it is actually about obtaining the previous product object, so -1 seems the right wayget_stock_status()
you can determine the status of the previous product and based on that make the current product purchasableSo you get: