在Woocommerce 3中仅允许购买一件商品 [英] Only allow to purchase one Item In Woocommerce 3

查看:86
本文介绍了在Woocommerce 3中仅允许购买一件商品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,有没有阻止在WooCommerce中购买一项以上的商品,还是阻止一项以上的商品添加到购物车?

Is there anyway to prevent more than one item for purchase in WooCommerce, or prevent more than one item added to the cart?

我有不同的产品,但我想要每次结帐只允许一个项目。

I have different products but I want to allow only one item per checkout.

我尝试搜索解决方案,但那些现有解决方案无法正常工作,比方说,当用户未登录并添加项目时放入购物车,然后转到结帐并登录,那里有一个以前在客户登录时已添加的商品,也累加到刚添加的一个客户,所以现在购物车内有2个产品,这是这里的问题我正在使用的代码无法正常工作。

I tried to search for solution but those existing solutions are not working properly, let's say when user is not signed in and adds an item into cart and then goes to checkout and logins there an item that has been added previously when customer was logged in also adds up to the one customer just added, so now there is 2 products inside the cart, and this is an issue here is the code that I'm using that's not working properly.

function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );


推荐答案

更新的(根据要求提供第二种选择在您的评论中)。

下面的代码将把添加到购物车限制为一个唯一的项目,当添加多个项目时会显示错误消息。第二个功能将检查购物车中的物品,避免结帐,并在有多个物品时添加错误消息:

The code below will limit add to cart to a unique item displaying an error message when adding more than one. A second function will check cart items avoiding checkout and adding an error message when there is more than one item:

// Allowing adding only one unique item to cart and displaying an error message
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 1 );
function add_to_cart_validation( $passed ) {
    if( ! WC()->cart->is_empty() ){
        wc_add_notice( __("You can add only one item to cart", "woocommerce" ), 'error' );
        $passed = false;
    }
    return $passed;
}

// Avoiding checkout when there is more than one item and displaying an error message
add_action( 'woocommerce_check_cart_items', 'check_cart_items' ); // Cart and Checkout
function check_cart_items() {
    if( sizeof( WC()->cart->get_cart() ) > 1 ){
        // Display an error message
        wc_add_notice( __("More than one items in cart is not allowed to checkout", "woocommece"), 'error' );
    }
}

代码会出现在您活跃孩子的functions.php文件中主题(或活动主题)。经过测试并有效。

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

1)尝试将第二项添加到购物车时:

2)如果一个以上项目在购物车中:

3)并在结帐时,您将获得一个带有相同错误通知的空白页面:

3) And in checkout you will get an empty page with the same error notice:

仅允许一个购物车中的物品在任何情况下都可以使用:

To allow only one cart item removing any additional items that will work in any cases:

// Removing on add to cart if an item is already in cart
add_filter( 'woocommerce_add_cart_item_data', 'remove_before_add_to_cart' );
function remove_before_add_to_cart( $cart_item_data ) {
    WC()->cart->empty_cart();
    return $cart_item_data;
}

// Removing one item on cart item check if there is more than 1 item in cart
add_action( 'template_redirect', 'checking_cart_items' ); // Cart and Checkout
function checking_cart_items() {
    if( sizeof( WC()->cart->get_cart() ) > 1 ){
        $cart_items_keys = array_keys(WC()->cart->get_cart());
        WC()->cart->remove_cart_item($cart_items_keys[0]);
    }
}

代码会出现在您活跃孩子的functions.php文件中主题(或活动主题)。经过测试,可以正常工作。

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于在Woocommerce 3中仅允许购买一件商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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