Php 购物车中每个产品类别只允许一个产品
在此基础上,我找到了关于如何在Woocommerce的购物车中为每个类别添加一个产品的PHP代码 代码工作正常,但我想将最新添加的产品添加到购物车,如果购物车中已经有该类别的产品,我想删除最旧的产品Php 购物车中每个产品类别只允许一个产品,php,wordpress,woocommerce,cart,categories,Php,Wordpress,Woocommerce,Cart,Categories,在此基础上,我找到了关于如何在Woocommerce的购物车中为每个类别添加一个产品的PHP代码 代码工作正常,但我想将最新添加的产品添加到购物车,如果购物车中已经有该类别的产品,我想删除最旧的产品 add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 ); function custom_checking_product_added_to_cart( $p
add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {
// HERE Type your alert displayed message
// $message = __( 'BLA BLA YOUR MESSAGE.', 'woocommerce' );
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat){
$product_cats[] = $obj_prod_cat->slug;
}
foreach (WC()->cart->get_cart() as $cart_item ){
if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {
$passed = false;
wc_add_notice( $message, 'error' );
break;
}
}
return $passed;
}
这是一段源代码。它工作正常,但我需要一个额外的选择
在我的Woocommerce中,有5个不同的类别,每个类别只需在购物车中放置一个项目。最新添加的项目应保留,已在购物车中的同一类别的项目必须从购物车中删除。
你们中有人有解决办法吗
非常感谢 这是您的代码,它将删除与当前产品类别匹配的购物车项目
add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {
// Getting the product categories slugs in an array for the current product
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat)
$product_cats[] = $obj_prod_cat->slug;
// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// When the product category of the current product match with a cart item
if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] ))
{
// Removing the cart item
WC()->cart->remove_cart_item($cart_item_key);
// Displaying a message
wc_add_notice( 'Only one product from a category is allowed in cart', 'notice' );
// We stop the loop
break;
}
}
return $passed;
}
这段代码位于活动子主题(或主题)的function.php文件或任何插件文件中
此代码经过测试,适用于WooCommerce版本2.6+和3.0+以下是您的代码,它将删除与当前产品类别匹配的购物车项目
add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {
// Getting the product categories slugs in an array for the current product
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat)
$product_cats[] = $obj_prod_cat->slug;
// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// When the product category of the current product match with a cart item
if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] ))
{
// Removing the cart item
WC()->cart->remove_cart_item($cart_item_key);
// Displaying a message
wc_add_notice( 'Only one product from a category is allowed in cart', 'notice' );
// We stop the loop
break;
}
}
return $passed;
}
这段代码位于活动子主题(或主题)的function.php文件或任何插件文件中
此代码经过测试,适用于WooCommerce版本2.6+和3.0+请说明您使用的是哪个版本的WooCommerce?请说明您使用的是哪个版本的WooCommerce?