這可以通過使用woocommerce_checkout_process
和woocommerce_before_cart
Woocommerce Hooks來完成。
因此,在你的主題functions.php
file(改姓名類的字符串)添加以下代碼:
add_action('woocommerce_checkout_process', 'wc_minimum_order_amount');
add_action('woocommerce_before_cart' , 'wc_minimum_order_amount');
function wc_minimum_order_amount() {
$minimum = 50; //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("Name Your category", $draught_links)){
$on_draught = true;
}else{
$on_draught = false;
}
if(is_cart()) {
if($on_draught){
wc_print_notice(
sprintf('You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
$minimum ,
WC()->cart->cart_contents_count
), 'error'
);
}
} else {
if($on_draught){
wc_add_notice(
sprintf('You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
$minimum ,
WC()->cart->cart_contents_count
), 'error'
);
}
}
}
}
你似乎有了答案已經存在的代碼,所以有什麼問題嗎? – Johan
上述代碼基於貨幣價值計算整個購物車總額 - 我所追求的是來自特定類別的產品的最低數量,而不考慮價格。 – Mike