强制以单独的顺序出售特定的WooCommerce产品 [英] Force specific WooCommerce product to be sold in separate order
问题描述
我试图强制WooCommerce中的特定产品单独出售.但是,我希望该产品无限量出售.
I'm trying to force a specific product in WooCommerce to be sold separately. I however want this product to sell in unlimited quantity.
基于强制单独出售要在WooCommerce中单独购买的产品效果很好,我目前正在使用:
Based on Force sold individually product to be bought alone in WooCommerce answer code which works quite well, I am currently using:
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Product id to bought alone
$product_id_alone = 666;
// Set variable
$alone = true;
// If passed
if ( $passed ) {
// If cart is NOT empty when a product is added
if ( !WC()->cart->is_empty() ) {
// If product id added = product id alone
if ( $product_id_alone == $product_id ) {
$alone = false;
} else {
// Generate a unique ID for the cart item
$product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );
// Check if product is in the cart
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
// If product is already in cart
if ( $in_cart ) {
$alone = false;
}
}
} else {
if ( $product_id_alone == $product_id) {
$alone = true;
}
}
}
if ( $alone == false ) {
// Set error message
$message = 'Product 666 must be bought separately.';
wc_add_notice( __( $message, 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
如果购物车是空的,我可以添加ID 666和自定义数量的产品.
If the cart is empty, I can add product with ID 666 with a custom quantity.
一旦将商品ID 666添加到购物车中,便无法将其他商品添加到购物车中.
Once product ID 666 has been added to the cart, I can't add another product to the cart.
如果我先将其他产品添加到空购物车中,则无法将产品ID 666添加到购物车中.
And if I start by adding another product to an empty cart, I can't add product ID 666 to the cart.
问题是,如果我将产品ID 666添加到空购物车中,则无法通过向购物车中添加更多产品来增加产品666的数量.
The issue is that if I add product ID 666 to an empty cart I can't increase quantity of product 666 by adding more of that product into the cart.
推荐答案
要强制以单独的顺序单独出售特定产品,请使用:
To force a specific product to be sold alone, in a separate order, use:
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Product id to bought alone
$product_id_alone = 666;
// Set variable
$flag = false;
// If cart is NOT empty when a product is added
if ( ! WC()->cart->is_empty() ) {
// Generate a unique ID for the cart item
$product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );
// Check if product is in the cart
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
// If product is already in cart & product ID added is not equal to product ID alone
if ( $in_cart && ( $product_id != $product_id_alone ) ) {
$flag = true;
// Product ID alone is NOT in cart & product ID added is equal to product ID alone
} elseif( ! $in_cart && ( $product_id == $product_id_alone ) ) {
$flag = true;
}
}
// True
if ( $flag ) {
// Set error message
wc_add_notice( sprintf(
__( 'Product %s must be bought separately', 'woocommerce' ),
$product_id_alone,
), 'error' );
// Passed = false
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
添加ID为666的产品时:
When adding the product with ID 666:
- 如果购物车为空,则可以添加任意数量的产品.
- 如果购物车不为空&ID为666的产品不在购物车中,请停止.
- 如果购物车不为空&ID为666的产品在购物车中,继续.
添加具有不同ID的产品时:
When a product with a different ID is added:
- 如果购物车为空,则可以添加任意数量的产品.
- 如果购物车不为空&ID为666的产品不在购物车中,继续.
- 如果购物车不为空&ID为666的产品在购物车中,请停止.
这篇关于强制以单独的顺序出售特定的WooCommerce产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!