限制 Woocommerce 中购物车项目的数量 [英] Limit the number of cart items in Woocommerce
问题描述
我正在使用 Woocommerce 并需要以下内容:
I am using Woocommerce and need the following:
由于产品要销往另一个国家,而该国的海关只允许总数量为 6,因此我需要防止客户订购超过 6 件(产品).
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 是项目或产品的总数.客户可以订购 1 件产品,数量为 6 件或 2 件产品,每件数量为 3 件.海关只允许物品总数为 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屋!