Woocommerce:整体产品类别的最小数量,而不是变体
Woocommerce: Minimum Quantity for Overall Product Category, not variations
所以,我已经安装了一个插件 ("Advanced Product Quantities"),它允许我设置类别和产品的最低数量。但是,我想要做的是为整体 product/category 设置最小数量,而不是为每个单独的变体设置最小数量。为了说明这一点,我有一家出售百吉饼的烘焙食品公司。一位顾客至少需要订购 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;
}
}
所以,我已经安装了一个插件 ("Advanced Product Quantities"),它允许我设置类别和产品的最低数量。但是,我想要做的是为整体 product/category 设置最小数量,而不是为每个单独的变体设置最小数量。为了说明这一点,我有一家出售百吉饼的烘焙食品公司。一位顾客至少需要订购 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;
}
}