购物车中每个产品类别只允许一个产品 [英] Allow only one product per product category in cart

查看:81
本文介绍了购物车中每个产品类别只允许一个产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此> 问题/解答 我发现了有关如何在Woocommerce中为购物车中的每个类别添加一个产品的PHP代码。

On this Question / Answer I have found the PHP code about how to just add one product per category in cart in Woocommerce.

代码工作正常,但是我想将最新添加的产品添加到购物车中,如果购物车中已经有该类别的产品,我希望最旧的已删除。

The code works just fine, but I want to add the latest added product to cart and if there is already a product of that category in the cart, I want to have the oldest deleted.

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;
}

这是来自 LoicTheAztec 。它工作正常,但我需要一个额外的选择...

It is a piece of code that comes from LoicTheAztec . It works fine but I need an extra option...

在我的Woocommerce中,有5种不同的类别,每个类别中只有一个放在购物车中。最新添加的项目应保留,必须从购物车中删除购物车中已经存在的相同类别的项目。
你们中的某人有解决方案吗?

In my Woocommerce there are 5 different categories, there is just place in the cart for one item per category. The latest added item should stay, the item of the same category that is already in cart must be removed from cart. Does someone of you has a solution?

非常感谢!

推荐答案

这是您可以放心使用的代码

Here is your peace of code that will remove a cart item which product category match with the current product category.

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 +

This code is tested and works for WooCommerce version 2.6+ and 3.0+

这篇关于购物车中每个产品类别只允许一个产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆