[英]Woocommerce: How to allow only one product type in a cart?
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']
.我有两种产品类型,一种是简单产品
$cart_item['default-engraving']
,另一种是信用产品$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.更新:无法在添加到购物车事件时检测自定义购物车项目数据。
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:检查购物车项目将允许您防止购物车项目同时具有
$cart_item['default-engraving']
和$cart_item['iconic-engraving']
:
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' );
}
}
Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.
测试和工作。
Original answer : (it can't work for your case as it's not possible to detect that on add to cart event)原始答案:(它不适用于您的情况,因为在添加到购物车事件时无法检测到该情况)
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;
}
Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.
测试和工作。
Related: Allow only one product category in cart at once in Woocommerce相关: 在 Woocommerce 中一次只允许购物车中的一个产品类别
You may try this你可以试试这个
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
// Getting the product categories term slugs in an array for the current product
$term_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the product category of the current product don't match with a cart item
$_product = wc_get_product( $cart_item['product_id']; );
if( $_product->is_type( 'simple' ) ) {
// code to not allow other product type
} else {
// do stuff for everything else
}
}
}
please check below code.请检查下面的代码。
<?php
function ji_woocommerce_add_to_cart_validation( $valid, $product_id, $quantity )
{
$_product = wc_get_product( $product_id);
foreach (WC()->cart->get_cart() as $item ){
$checkTypes[] = wc_get_product( $item['product_id'] )->get_type();
}
$checkTypes = array_unique( $types );
if(count($checkTypes) > 0)
{
if(!in_array($_product->get_type(),$checkTypes))
{
wc_add_notice( __('Here you can add your error message','textdomain'), 'error' );
return false;
}
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'ji_woocommerce_add_to_cart_validation', 10, 3 );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.