限制 woocommerce 购物车中的项目数量?
Capping number of items in woocommerce cart?
我想将购物车中的产品数量限制为最多 6。
我的 woocommerce 设置中的所有产品都是单独销售的(不能购买同一产品的多个)。
购物车示例
有没有办法将购物车限制为 6 个产品?因此,如果用户去添加第 7 个,则会弹出警告 'Maximum of 6 products only. Please remove a product from your cart'?
尝试谷歌搜索,但只能找到这个,仅限于单一产品:Need Woocommerce to only allow 1 product in the cart
这与您链接到的其他问题的过程相同。在验证过滤器上,检查购物车内容,如果不满足您的要求,则 return 一条消息。如前所述,您确实会使用 get_cart_contents_count()
来计算购物车中的订单项数量。
已编辑: 删除 empty()
购物车检查
function so_31516355_cap_cart_count( $valid, $product_id, $quantity ) {
if( $valid && ( $quantity > 6 || intval( WC()->cart->get_cart_contents_count() ) > 6 || $quantity + intval( WC()->cart->get_cart_contents_count() ) > 6 ) ){
$valid = false;
wc_add_notice( 'Whoa hold up. You can only have 6 items in your cart', 'error' );
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'so_31516355_cap_cart_count', 10, 3 );
已添加: 购物车更新验证
function so_31516355_cap_cart_count_update( $valid, $cart_item_key, $values, $quantity ) {
if( $valid && ( $quantity > 6 || intval( WC()->cart->get_cart_contents_count() ) > 6 || $quantity + intval( WC()->cart->get_cart_contents_count() ) > 6 ) ){
$valid = false;
wc_add_notice( __( 'Whoa hold up. You can only have 6 items in your cart', 'your-plugin' ), 'error' );
}
return $valid;
}
add_filter( 'woocommerce_update_cart_validation', 'so_31516355_cap_cart_count_update', 10, 4 );
我想将购物车中的产品数量限制为最多 6。 我的 woocommerce 设置中的所有产品都是单独销售的(不能购买同一产品的多个)。
购物车示例
有没有办法将购物车限制为 6 个产品?因此,如果用户去添加第 7 个,则会弹出警告 'Maximum of 6 products only. Please remove a product from your cart'?
尝试谷歌搜索,但只能找到这个,仅限于单一产品:Need Woocommerce to only allow 1 product in the cart
这与您链接到的其他问题的过程相同。在验证过滤器上,检查购物车内容,如果不满足您的要求,则 return 一条消息。如前所述,您确实会使用 get_cart_contents_count()
来计算购物车中的订单项数量。
已编辑: 删除 empty()
购物车检查
function so_31516355_cap_cart_count( $valid, $product_id, $quantity ) {
if( $valid && ( $quantity > 6 || intval( WC()->cart->get_cart_contents_count() ) > 6 || $quantity + intval( WC()->cart->get_cart_contents_count() ) > 6 ) ){
$valid = false;
wc_add_notice( 'Whoa hold up. You can only have 6 items in your cart', 'error' );
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'so_31516355_cap_cart_count', 10, 3 );
已添加: 购物车更新验证
function so_31516355_cap_cart_count_update( $valid, $cart_item_key, $values, $quantity ) {
if( $valid && ( $quantity > 6 || intval( WC()->cart->get_cart_contents_count() ) > 6 || $quantity + intval( WC()->cart->get_cart_contents_count() ) > 6 ) ){
$valid = false;
wc_add_notice( __( 'Whoa hold up. You can only have 6 items in your cart', 'your-plugin' ), 'error' );
}
return $valid;
}
add_filter( 'woocommerce_update_cart_validation', 'so_31516355_cap_cart_count_update', 10, 4 );