限制Woocommerce中的购物车数量 [英] Limit the number of cart items in Woocommerce

查看:347
本文介绍了限制Woocommerce中的购物车数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Woocommerce,并且需要以下条件:

I am using Woocommerce and need the following:

  1. 由于产品要卖到另一个国家,并且那个国家的海关总共只允许6个商品,所以我需要防止客户订购超过6个商品(产品).

  1. As product is being sold to another country and that country's customs only allow a total quantity of 6, so I need to prevent customers from order more than 6 items (products).

6是项目或产品的总数.客户可以订购6种数量的1种产品,也可以订购3种数量的2种产品.海关只允许物品总数为6.

6 is the total of items or products. Customer can order 1 product in quantity of 6 or 2 products with quantity of 3 each. Customs will only allow a total number of items to be 6.

如果购物车中有6件以上,则会出现警告,并阻止客户继续结帐.

If there are more then 6 items in the cart, a warning should appear and prevent customer from proceeding to check out.

是否可以将购物车中的物品限制为6个,并且在超过此限制时显示一条消息?

Is this possible to limit cart items to 6 and to display a message when this limit is exceeded?

推荐答案

有2个动作可以检查和控制是否要限制购物车商品:

There is 2 actions to check and to control if you want to limit cart items:

  • 将产品添加到购物车时(在商店页面和产品页面)
  • 当购物车页面中的数量更新时

使用连接到 woocommerce_add_to_cart_validation 过滤器挂钩的自定义功能,您可以将购物车中的商品限制为最多6个,并在超出此限制时显示自定义消息:

Using a custom function hooked in woocommerce_add_to_cart_validation filter hook, will allow you to restrict the cart items to 6 max and to display a custom message when this limit is exceeded:

// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'only_six_items_allowed_add_to_cart', 10, 3 );

function only_six_items_allowed_add_to_cart( $passed, $product_id, $quantity ) {

    $cart_items_count = WC()->cart->get_cart_contents_count();
    $total_count = $cart_items_count + $quantity;

    if( $cart_items_count >= 6 || $total_count > 6 ){
        // Set to false
        $passed = false;
        // Display a message
         wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
    }
    return $passed;
}

使用连接到 woocommerce_update_cart_validation 过滤器挂钩的自定义功能,您可以控制购物车项目数量更新到6个购物车项目限制,并在超过此限制时显示自定义消息:

Using a custom function hooked in woocommerce_update_cart_validation filter hook, will allow you to control the cart items quantities update to your 6 cart items limit and to display a custom message when this limit is exceeded:

// Checking and validating when updating cart item quantities when products are added to cart
add_filter( 'woocommerce_update_cart_validation', 'only_six_items_allowed_cart_update', 10, 4 );
function only_six_items_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {

    $cart_items_count = WC()->cart->get_cart_contents_count();
    $original_quantity = $values['quantity'];
    $total_count = $cart_items_count - $original_quantity + $updated_quantity;

    if( $cart_items_count > 6 || $total_count > 6 ){
        // Set to false
        $passed = false;
        // Display a message
         wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
    }
    return $passed;
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

此代码已经过测试并且可以正常工作

This code is tested and works

这篇关于限制Woocommerce中的购物车数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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