因此,我已经安装了一个插件(“高级产品数量”),它允许我为类别和产品设置最小数量。然而,我要做的是为整个产品/类别设定一个最小的数量,而不是为每个单独的变化设置最小的数量。为了更好地理解这一点,我有一家烘焙食品公司,在那里我出售百吉饼。顾客至少需要订购12个百吉饼。他们可以订购他们想要的任何种类的百吉饼(例如:4个罂粟籽,5个普通面包,3个洋葱等),只要他们至少订购12个百吉饼。
当我为百吉饼类设定一个最小类别时,它会使购物车中的每种产品都增加到12种来完成结账,而不是认识到它们都是一种百吉饼,因此满足了整个百吉饼最少12个百吉饼的要求。
有没有人知道我怎样才能让这个最小数量的概念适用于我的情况?
发布于 2015-01-20 06:44:35
试试下面的代码:
// 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;
}
}https://stackoverflow.com/questions/28037139
复制相似问题