有人可以帮忙吗?
我想限制不同类别的产品的最低数量。
EG:
产品 1、产品 2、产品 3(鞋履类别)- 至少 10 件组合
产品 4、产品 5、产品 6(裤子类别)- 至少 10 件组合
产品 7、产品 8、产品 9(衬衫类别)- 至少组合 10 件
每个类别应至少订购 10 件产品,但同一类别内可以有多个产品。
我发现了这段代码,它确实有效,它给出了最低限度的通知,但允许多个类别混合,从而违背了目的。
从多个类别中订购是可选的,但如果这样做,则必须是最少的。
add_action('woocommerce_check_cart_items', 'custom_set_min_total');
function custom_set_min_total()
{
if (is_cart() || is_checkout()) {
global $woocommerce, $product;
$i = 0;
foreach ($woocommerce->cart->cart_contents as $product) :
$minimum_cart_product_total = 10;
if (has_term( array('oneya', 'twoya'), 'product_cat', $product['product_id'])) :
$total_quantity += $product['quantity'];
endif;
endforeach;
foreach ($woocommerce->cart->cart_contents as $product) :
if (has_term( array('oneya', 'twoya'), 'product_cat', $product['product_id'])) :
if ($total_quantity < $minimum_cart_product_total && $i == 0) {
wc_add_notice(
sprintf(
'A Minimum of 10 products is required per unique design, you have not met these requirements, please add the minimum of 10 to proceed with your order',
$minimum_cart_product_total,
$total_quantity
),
'error'
);
}
$i++;
endif;
endforeach;
}
}
要按类别将商品限制为最小组合数量 (10),请尝试以下操作:
add_action('woocommerce_check_cart_items', 'minimum_item_quantity_by_category');
function minimum_item_quantity_by_category(){
// Your categories settings
$terms_count = ['Shoes' => 0, 'Pants' => 0, 'Shirt' => 0 ];
$terms_count = ['Hoodies' => 0, 'Accessories' => 0, 'Tshirts' => 0 ];
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
// Loop through categories
foreach ( array_keys($terms_count) as $category ) {
if ( has_term( $category, 'product_cat', $item['product_id']) ) {
$terms_count[$category] += $item['quantity'];
}
}
}
// Loop through category / count array
foreach ( $terms_count as $category => $count ) {
if ( $count != 0 && $count < 10 ) {
$term = get_term_by('name', $category, 'product_cat');
wc_add_notice( sprintf(
__('A Minimum of 10 items from %s category is required. Add %d more to proceed with your order.', 'woocommerce'),
'<a href="'.get_term_link($term).'">'.$category.'</a>', (10 - $count) ), 'error' );
}
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并有效。