我从冷藏订购了最少3件产品类别。
无已冷购物车中的产品类别和其他产品类别的任何组合(Bundle)周围,礼物,)在购物车中,客户应该能够结帐。
已冷购物车中的类别产品但是还有一个Bundle对购物车中的产品进行分类,客户应该仍然能够结帐。
我的wc_add_notice error
正在计算已冷却逻辑正确,但即使在冷冻中没有项目时,它也会显示。目录。
请有人帮助我哪里出错了?
// Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'chilled_set_min_total' );
function chilled_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_chilled = 3;
$total_chilled = 0;
$cat_in_cart = false;
//check if chilled product in cart. If exists add to quantity
foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( has_term( 'chilled', 'product_cat', $product['product_id'] ) ) {
$total_chilled += $product['quantity'];
}
if ( has_term( 'bundles', 'product_cat', $cart_item['product_id'] ) ) {
$cat_in_cart = true;
break;
}
endforeach;
//if chilled items is less than minimum display error
if ($total_chilled < $minimum_chilled) {
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_chilled,
$total_chilled ),
'error' );
}
}
}
这一行的问题
if ($ total_frozen <minimum_chilled美元){>
如果我们没有冷冻,那么$ total_冷冻不改变并且= 0
我们有0 <3和它总是= true
要解决这个问题,你需要增加一个检查
//Check if there are any products from this category in the cart
if ($total_chilled > 0) {
//if chilled items is less than minimum display error
if ($total_chilled < $minimum_chilled) {
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_chilled,
$total_chilled ),
'error' );
}
}
更新,如果你需要禁用checkout按钮添加
remove_action(' woocommerce_proced_to_checkout ', ' woocommerce_button_proced_to_checkout ', 20);
//Check if there are any products from this category in the cart
if ($total_chilled > 0) {
//if chilled items is less than minimum display error
if ($total_chilled < $minimum_chilled) {
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_chilled,
$total_chilled ),
'error' );
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}