Woocommerce:如何在购物车中仅允许一种产品类型? [英] Woocommerce: How to allow only one product type in a cart?
问题描述
我有两种产品类型,一种是简单产品 $ cart_item ['default-engraving']
,另一种是信用产品 $ cart_item ['default-engraving']&&$ cart_item ['iconic-engraving']
.我正在尝试找到一种解决方案,如果我将简单的产品添加到购物车中,则不应该添加信用产品.或者,如果我将信用产品添加到购物车,则不应添加简单产品.但是,如果我愿意,我可以根据需要添加相同类型,例如简单产品类型.
I have two product types, one of them simple product $cart_item['default-engraving']
and the other one credit product $cart_item['default-engraving'] && $cart_item['iconic-engraving']
. I'm trying to find a solution how to make that if I add simple product to a cart, credit product shouldn't be added. Or if I add credit product to a cart, simple product should't be added. But if I want I could add same type for example simple product type as many as I want.
推荐答案
更新:无法在添加到购物车事件中检测到自定义购物车数据.
Update: Is not possible to detect custom cart item data on add to cart event.
通过检查购物车商品,您可以防止购物车商品的 $ cart_item ['default-engraving']
和 $ cart_item ['iconic-engraving']
同时:
Checking cart items will allow you to prevent having cart items that have $cart_item['default-engraving']
and $cart_item['iconic-engraving']
at the same time:
add_action( 'woocommerce_check_cart_items', 'check_cart_items_custom_data' );
function check_cart_items_custom_data() {
// Initializing: set the current product type in an array
$types = [];
// Loop through cart items
foreach (WC()->cart->get_cart() as $item ){
if( isset( $item['default-engraving'] ) )
$types[] = 'default';
if( isset( $item['iconic-engraving'] ) )
$types[] = 'iconic';
}
$types = array_unique( $types );
// Check the number of product types allowing only one
if( count( $types ) > 1 ){
// Displaying a custom notice and avoid checkout
wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
}
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试并可以正常工作.
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
原始答案:(由于您无法在添加到购物车事件中检测到该情况,因此无法解决您的情况)
以下是将产品类型定位为仅允许购物车中的一种的方法:
Here is the way targeting the product type to allow only one in cart:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_type_allowed', 10, 3 );
function only_one_product_type_allowed( $passed, $product_id, $quantity ) {
// Initializing: set the current product type in an array
$types = [ wc_get_product( $product_id )->get_type() ];
// Loop through cart items
foreach (WC()->cart->get_cart() as $item ){
// Set each product type in the array
$types[] = wc_get_product( $item['product_id'] )->get_type();
}
$types = array_unique( $types );
// Check the number of product types allowing only one
if( count( $types ) > 1 ){
// Displaying a custom notice
wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
return false; // Avoid add to cart
}
return $passed;
}
代码进入活动子主题(或活动主题)的functions.php文件中.经过测试并可以正常工作.
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
相关:允许Woocommerce中购物车中一次只有一个产品类别
这篇关于Woocommerce:如何在购物车中仅允许一种产品类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!