![](/img/trans.png)
[英]WooCommerce: Set minimum order amount for just specific product categories
[英]Set minimum Order amount for specific Products or Categories in WooCommerce
我已經廣泛搜索,看看其他人是否有這種情況,但沒有運氣就得到了答案。
本質上,我有兩個項目客戶可以添加到他們的購物車中。 我想這樣做,如果他們的小計不是 15 美元或更多,他們就無法結賬。
能夠將他們的 ID 放入代碼中就好了。 或者,我可以將它們分配到同一類別並按類別設置此最小值。
到目前為止,我所擁有的只是設置通用最小值的基本 PHP。 我只需要一些幫助來弄清楚如何限制它以滿足我的需求。
我是設計師而不是開發人員,因此非常感謝任何幫助。
// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set minimum cart total
$minimum_cart_total = 10;
// Total we are going to be using for the Math
// This is before taxes and shipping charges
$total = WC()->cart->subtotal;
// Compare values and add an error is Cart's total
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Minimum of 10 USD is required before checking out. (Cont. below)
// Current cart total: 6 USD
if( $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
.'<br />Current cart\'s total: %s %s',
$minimum_cart_total,
get_option( 'woocommerce_currency'),
$total,
get_option( 'woocommerce_currency') ),
'error' );
}
}
}
2020 更新
僅為購物車和結帳頁面設置購物車中某些產品類別或產品 ID 的最小值。
這個未經測試的片段由來自 wooThemes 的這個和這個組成,還有這個:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
## SETTINGS ##
$minimum_amount = 50; // Define a minimum order amount
$category_ids = array( 17, 18 ); // Define your category ids in the array (or an empty array to disable)
$product_ids = array( 64, 67, 78 ); // Define your product ids in the array (or an empty array to disable)
// Only on cart or checkout pages
if( WC()->cart->is_empty() || ! ( is_cart() || is_checkout() ) )
return; // Exit
$total_amount = WC()->cart->subtotal; // Items subtotal including taxes
if ( $total_amount < $minimum_amount ) {
$needs_minimum_amount = false; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
// 1. Check for matching product categories
if( sizeof($category_ids) > 0 ) {
$taxonomy = 'product_cat';
if ( has_term( $category_ids, $taxonomy, $product_id ) ) {
$needs_minimum_amount = true;
break; // Stop the loop
}
}
// 2. Check for matching product Ids
if( sizeof($product_ids) > 0 ) {
if ( array_intersect( $product_ids, array($product_id, $variation_id) ) ) {
$needs_minimum_amount = true;
break; // Stop the loop
}
}
}
if( $needs_minimum_amount ) {
wc_print_notice( sprintf(
__("You must have an order with a minimum of %s to place your order. Your current order total is %s."),
wc_price( $minimum_amount ),
wc_price( $total_amount )
), 'error' );
}
}
}
代碼位於您的活動子主題(活動主題)的 functions.php 文件中。 測試和工作。
您可以設置類別 ID、產品 ID 和最小訂單金額。
— — ……我很重要……注意…… — —
此掛鈎適用於消息。 但是為了避免用戶點擊,您需要添加 Javascript/jQuery,也可能使用 ajax(客戶端檢測)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.