在WooCommerce中,我需要为每个产品类别的商品设置一个最小数量。我搜索了论坛,找到了一些代码,除了它只计算产品类别的总数量外,其他代码都很好用:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
$minimum = 5; //Qty product
if ( WC()->cart->cart_contents_count < $minimum ) {
$draught_links = array();
foreach(WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
foreach ($terms as $term) {
$draught_links[] = $term->name;
}
}
if (in_array("Noten", $draught_links)){
$on_draught = true;
}else{
$on_draught = false;
}
if( is_cart() ) {
if($on_draught){
wc_print_notice(
sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' ,
$minimum ,
WC()->cart->cart_contents_count
), 'error'
);
}
} else {
if($on_draught){
wc_add_notice(
sprintf( 'Bitte beachte die Mindestbestellmenge. Du brauchst mindestens %s Notenexemplare pro Arrangement. Aktuell hast du %s Stück in deinem Warenkorb.' ,
$minimum ,
WC()->cart->cart_contents_count
), 'error'
);
}
}
}
}
例如,如果我有两个属于同一产品类别的产品(A和B),并将此类别的最小数量设置为5,则在这种情况下不会显示客户的错误消息:
- 产品A:3个
- 产品B:2个
我需要一个最小数量为5的每一个单一的产品的类别。
您知道如何更改和优化以下代码吗?
3条答案
按热度按时间kjthegm61#
由于WooCommerce 3,你的实际代码是过时的,不方便...有多种方法:
1).**最佳方法:**在产品级别设置最小数量(对于产品类别):
代码进入你的活动子主题(或活动主题)的functions.php文件。测试和工作。
2).替代方式:检查购物车项目并显示错误消息 (类似于您的代码):
代码进入你的活动子主题(或活动主题)的functions.php文件。测试和工作。
要使其也适用于父产品类别,您还需要添加此自定义函数:
代码进入你的活动子主题(或活动主题)的functions.php文件。测试和工作。
然后在现有代码中,您将替换:
由
这将允许您处理父产品类别太多。
8yoxcaq72#
要使其适用于某个产品类别的项目总数,请使用以下代码:
xtupzzrd3#
我正在使用此代码,但有2个问题.
}
Ps:我对编程几乎一无所知,谢谢各位的帮助。