简体   繁体   English

Woocommerce购物车中仅允许两种产品中的一种

[英]Allow only one from two products in Woocommerce cart

I have two grouped products A and B : 我有两个分组的产品AB

1) Product A 1)产品A

  • Woocommerce subscription functionality Alpha ( monthly subscription) Woocommerce订阅功能Alpha( 每月订阅)
  • Woocommerce subscription platform (annual subscription) Woocommerce订阅平台(年度订阅)

2) Product B 2)产品B

  • Woocommerce subscription functionality Alpha ( annual subscription) Woocommerce订阅功能Alpha( 年度订阅)
  • Woocommerce subscription platform (annual subscription) Woocommerce订阅平台(年度订阅)

The difference is in the subscription Alfa. 区别在于订阅Alfa。

Problem 问题

I want that "Alpha" appears only once in the cart (monthly or annual). 我希望“ Alpha”仅在购物车中出现一次(每月或每年)。

Current status 当前状态

If a user adds to the cart "A" (it has an alpha monthly + platform per year). 如果用户将购物车“ A”添加到购物车(每月有一个alpha版本+每年一个平台)。 Then, if it changes his mind and clicks on "B", the system will tell him that the platform cannot be added once again, but alpha annually is added - that means that alpha is in the cart twice, which is not ok. 然后,如果改变主意并单击“ B”,系统将告诉他不能再次添加该平台,但是每年添加一次alpha-这意味着alpha在购物车中两次,这是不可行的。

There is also one X product - separate "x subscription", which can be ordered (not a group product). 还有一个X产品-单独的“ x订阅”可以订购(不是组产品)。

One solution that I see is that prior to adding to the basket, the system should first empty the basket. 我看到的一个解决方案是,在添加到购物篮之前,系统应首先清空购物篮。

A possible solution is also to check if a specific product is already in the cart (eg alpha - monthly) and remove it and add a new one (the one that is passed when the user clicked on add to cart). 一种可能的解决方案是检查购物车中是否已经有特定产品(例如Alpha-每月),然后将其删除并添加新产品(当用户单击“添加到购物车”时通过的产品)。

I found one code on the web (that goes into functions.php). 我在网上找到了一个代码(该代码进入functions.php)。 Function empties the cart but empties it for every added product. Function清空购物车,但为每个添加的产品清空。 So, in the cart, then only "platform" is left (because the system probably empties the cart, adds the alpha, then empties the cart again and then adds the platform). 因此,在购物车中只剩下“平台”(因为系统可能会清空购物车,添加alpha,然后再次清空购物车,然后添加平台)。

Here is the code: 这是代码:

add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);

function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) 
{
    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

How to allow only one from that 2 specific products to be in cart? 如何只允许这2种特定产品中的一种放入购物车?

The code below is based on your 2 "Alpha" subscriptions product IDs. 下面的代码基于您的2个“ Alpha”订阅产品ID。 It will remove the first product added to cart if both products are in cart. 如果两个产品都在购物车中,它将删除添加到购物车的第一个产品。 You will have to set in this function both products IDs: 您必须在此功能中设置两个产品ID:

add_action('woocommerce_before_calculate_totals', 'checking_and_removing_cart_items', 20, 1 );
function checking_and_removing_cart_items( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // HERE below define your "Alpha" products
    $alpha_monthly_id = 37;
    $alpha_yearly_id = 53;
    $found_monthly = $found_yearly = false;
    $keys = array();

    // Checking cart items
    foreach( $cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $alpha_monthly_id ){
            $found_monthly = true;
            $keys[] = $key;
        } elseif( $cart_item['product_id'] == $alpha_yearly_id ){
            $found_yearly = true;
            $keys[] = $key;
        }
    }
    // If both product found, we remove the first one
    if( $found_monthly && $found_yearly ){
        $cart->remove_cart_item($keys[0]);

        // (optional) Remove notices before they get displayed
        if( ! is_checkout() )
            wc_clear_notices(); 
    }
}

Code goes in function.php file of the active child theme (or active theme). 代码进入活动子主题(或活动主题)的function.php文件中。

Tested and works 经过测试和工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce:只允许购物车中相同类别的产品 - WooCommerce: Only allow products from the same category in cart 在WooCommerce中仅允许从相同父类别的产品添加到购物车 - Only allow add to cart from products of the same parent category in WooCommerce 仅从一种自定义分类法中限制购物车中的 WooCommerce 产品 - Limit WooCommerce products in cart only from one custom taxonomy Woocommerce 中定义的产品类别中只允许购物车中的一件商品 - Allow only one item in cart from defined product categories in Woocommerce 插件:Woocommerce > 如何允许将作者的产品添加到购物车 - Plugin: Woocommerce > How to allow add to cart products from author woocommerce交叉销售仅在购物车页面上显示两个随机产品 - woocommerce cross sell is only showing two random products on cart page 仅在购物车中保留 WooCommerce 可变产品的最后一个变化 - Only keep in cart the last variation from WooCommerce variable products 我想限制它只添加一个类别的产品,不允许其他人使用 woocommerce - I want to limit it to add only products from one category and not allow others with woocommerce 在Woocommerce中仅允许更新一个商品类别的购物车商品数量 - Allow cart item quantity update for only one product category in Woocommerce Woocommerce:如何在购物车中只允许一种产品类型? - Woocommerce: How to allow only one product type in a cart?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM