问题描述
让我们说我有一个产品A,可以单独购买.
Let's Say I have Product A which is allow to bought alone.
如果购物车中已经有其他物品,它将显示该消息. 不允许".我尝试了各种方法,但无法为此找到合适的解决方案.
If something else already in cart it shows the message. "Not Allow". I tried various methods but not able to find proper solution for that.
当某人试图单击添加到购物车"按钮时,它必须通过代码检查是否允许购物车中的其他物品不允许放入购物车并显示消息.
When somebody tries to click on the "ADD TO CART" button then it must check via code that if other items in cart it is not allow to put in cart and shows the message.
产品A是允许单独购买的.
PRODUCT A is allow bought alone.
我尝试了类别比较,并且可以正常工作.但我只想使用产品ID.
I tried with the category comparison and it works. but I want to do only with the Product ID.
add_filter('woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other', 10, 5);
function dont_add_paint_to_cart_containing_other($validation, $product_id) {
// Set flag false until we find a product in cat paint
$cart_has_paint = false;
// Set $cat_check true if a cart item is in paint cat
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$products_ids = 137817;
$product = $cart_item['data'];
if (has_term('miscellaneous', 'product_cat', $product->id)) {
$cart_has_paint = true;
// break because we only need one "true" to matter here
break;
}
}
$product_is_paint = false;
if (has_term('miscellaneous', 'product_cat', $product_id)) {
$product_is_paint = true;
}
// Return true if cart empty
if (!WC()->cart->get_cart_contents_count() == 0) {
// If cart contains paint and product to be added is not paint, display error message and return false.
if ($cart_has_paint && !$product_is_paint) {
echo '<script type="text/javascript">';
echo ' alert("Hello,Sorry, "custom order" items must be purchased separately! To purchase this item, please either checkout with your current cart or remove any "custom order" items from your cart to enable you add the regular items.")'; //not showing an alert box.
echo '</script>';
$validation = false;
}
// If cart contains a product that is not paint and product to be added is paint, display error message and return false.
elseif (!$cart_has_paint && $product_is_paint) {
echo '<script type="text/javascript">';
echo ' alert("Sorry, "custom order" item must be purchased separately! To purchase any "custom order" items, please either checkout with your current cart or empty cart to enable you add the "custom order" items.")'; //not showing an alert box.
echo '</script>';
$validation = false;
}
}
// Otherwise, return true.
return $validation;
}
此代码仅适用于miscellaneous
类别,而我只允许使用产品ID ..而不是类别.
This code only works with the miscellaneous
category I want to allow only with the Product ID.. not category..
推荐答案
假定只能从产品A购买1个数量
Assuming that only 1 quantity can be purchased from product A
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Product id to bought alone
$product_id_alone = 30;
// 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 is added when cart is empty but $quantity > 1
if ( $product_id_alone == $product_id && $quantity > 1 ) {
$alone = false;
}
}
}
if ( $alone == false ) {
// Set error message
$message = 'PRODUCT A is allow bought alone.';
wc_add_notice( __( $message, 'woocommerce' ), 'error' );
$passed = false;
// Empty the cart
WC()->cart->empty_cart();
// Add specific product with quantity 1
WC()->cart->add_to_cart($product_id_alone, 1 );
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
这篇关于允许特定产品单独购买Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!