Prevent WooCommerce checkout if minimum quantity for a specific category is not reached(如果未达到特定类别的最低数量,则阻止WooCommerce结账)
本文介绍了如果未达到特定类别的最低数量,则阻止WooCommerce结账的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些购物车逻辑,如果购物车中有冷藏类别中的1件商品,它会检查至少3件商品。-这是经过测试的,并且工作正常。
但是,如果我从其他类别(环境)向购物车添加另一个项目,并且购物车中现在同时有冷藏和环境项目,则不再应用该规则。
如果购物车中也有环境物品,我如何确保应用了3冰镇最低限度规则?
// Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'protein_set_min_total' );
function protein_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce, $product;
$i=0;
// Set minimum product cart total
//And initialise product catergory counters
$minimum_protein = 3;
$total_protein = 0;
$cat_in_cart = false;
foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( has_term( 'chilled', 'product_cat', $product['product_id'] ) ) {
$total_protein += $product['quantity'];
}
endforeach;
if (has_term( 'chilled', 'product_cat', $product['product_id'] ) && $total_protein < $minimum_protein) {
wc_add_notice( sprintf( '<strong>A Minimum of %s products are required from The CHILLED category before checking out.</strong>'
. '<br />Current number of Chilled items in the cart: %s.',
$minimum_protein,
$total_protein ),
'error' );
}
/
沃梦达教程
本文标题为:如果未达到特定类别的最低数量,则阻止WooComm
基础教程推荐
猜你喜欢
- PHP 守护进程/worker 环境 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01