使用限制 Woocommerce答案中的购物车项目数量,我可以限制我商店购物车页面中的总产品数量。但我需要限制购物车中不同商品的数量,但保持数量不受限制并限制产品总价格金额。
例如,我的购物车中有 10 件商品,但产品总数量为 50,产品总价金额为 5000。如果我将购物车中的产品数量设置为 10,总价金额为 5000,我的客户将无法再添加一件产品到他们的购物车中。
我该怎么做?
您可以使用以下方法来限制允许添加到购物车的不同产品的数量:
// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'only_n_products_allowed_in_cart', 10, 3 );
function only_n_products_allowed_in_cart( $passed, $product_id, $quantity ) {
$items_limit = 10;
$total_count = count( WC()->cart->get_cart() ) + 1;
if( $total_count > $items_limit ){
// Set to false
$passed = false;
// Display a message
wc_add_notice( sprintf( __( "You can’t have more than %s different products in cart", "woocommerce" ), $items_limit ), "error" );
}
return $passed;
}
代码位于活动子主题(或活动主题)的 functions.php 文件中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句