gpt4 book ai didi

php - Woocommerce:整个产品类别的最小数量,而不是变体

转载 作者:行者123 更新时间:2023-12-02 21:20:25 25 4
gpt4 key购买 nike

所以,我已经安装了一个插件(“高级产品数量”),它允许我设置类别和产品的最小数量。然而,我想做的是为整个产品/类别设置最小数量,而不是为其每个单独变体设置最小数量。为了说明这一点,我有一家烘焙食品公司,卖百吉饼。顾客需要至少订购 12 个百吉饼。他们可以订购任意数量的各种百吉饼(例如:4 个罂粟籽、5 个原味、3 个洋葱等),只要他们总共订购至少 12 个百吉饼。

当我为百吉饼类别设置类别最小值时,它会使购物车中的每个产品变体将其数量增加到 12 种以完成结帐,而不是认识到它们都是百吉饼的一种类型,因此满足总体百吉饼最小值12 个百吉饼。

有人知道如何让这个最低数量概念适合我的情况吗?

最佳答案

试试这个代码:

    // Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'rohil_set_min_total' );
function rohil_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {

global $woocommerce, $product;
$i=0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) :


// Set minimum product cart total
$minimum_cart_product_total = 12;

// See if any product is from the bagel category or not
if ( has_term( 'bagels', 'product_cat', $product['product_id'] ) ) :

$total_quantity += $product['quantity'];

endif;

endforeach;

if( $total_quantity < $minimum_cart_product_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s products is required from bagel category before checking out.</strong>'
. '<br />Current number of items in the cart: %s.',
$minimum_cart_product_total,
$total_quantity ),
'error' );
}

}

}

编辑:

    // Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'rohil_set_min_total' );
function rohil_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {

global $woocommerce, $product;
$i=0;
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) :


// Set minimum product cart total
$minimum_cart_product_total = 12;

// See if any product is from the bagel category or not
if ( has_term( 'bagels', 'product_cat', $product['product_id'] ) ) :

$total_quantity += $product['quantity'];

endif;

endforeach;

if ( has_term( 'bagels', 'product_cat', $product['product_id'] ) ) :
if( $total_quantity < $minimum_cart_product_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s products is required from bagel category before checking out.</strong>'
. '<br />Current number of items in the cart: %s.',
$minimum_cart_product_total,
$total_quantity ),
'error' );
}
endif;

}

}

新编辑

    // Set minimum quantity per product before checking out
add_action( 'woocommerce_check_cart_items', 'rohil_set_min_total' );
function rohil_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {

global $woocommerce, $product;
$i=0;
//$prod_id_array = array();
//loop through all cart products
foreach ( $woocommerce->cart->cart_contents as $product ) :


// Set minimum product cart total
$minimum_cart_product_total = 12;

// See if any product is from the bagel category or not
if ( has_term( 'bagels', 'product_cat', $product['product_id'] ) ) :

$total_quantity += $product['quantity'];
//array_push($prod_id_array, $product['product_id']);
endif;

endforeach;

foreach ( $woocommerce->cart->cart_contents as $product ) :
if ( has_term( 'bagels', 'product_cat', $product['product_id'] ) ) :
if( $total_quantity < $minimum_cart_product_total && $i == 0 ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s products is required from bagel category before checking out.</strong>'
. '<br />Current number of items in the cart: %s.',
$minimum_cart_product_total,
$total_quantity ),
'error' );
}
$i++;
endif;
endforeach;
}

}

关于php - Woocommerce:整个产品类别的最小数量,而不是变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28037139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com