2017-05-09 52 views
0

當用戶在購物車上時,我想在最終訂單中輸入我的訂單,並希望在完成時驗證最小金額。需要乘以7.woocommerce的產品最小量乘以7

例如..我有20個商店的產品。最小產品數量是任何產品的7倍。

Ex: 
Product with ID:3 & Quantity: 5 
Product with ID:13 & Quantity: 2 
    => Total:7 = > Success 

Product with ID:2 & Quantity: 3 
Product with ID:6 & Quantity: 8 
    => Total: 11 => Fail 

Product with ID:1 & Quantity: 2 
Product with ID:5 & Quantity: 8 
Product with ID:11 & Quantity: 4 
    => Total: 14 => Success 

我該怎麼做?這裏是我已經試過

function wc_minimum_order_amount() { 
    global $woocommerce; 
    $minimum = 7; 
    if ($woocommerce->cart->get_cart_total() % $minimum != 0) { 
     $woocommerce->add_error(sprintf('You must have an order with a minimum of %s to place your order.' , $minimum)); 
    } 
} 

回答

0

修訂

你需要連接你的函數爲 woocommerce_checkout_process,做你的計算和 顯示錯誤。

事情是這樣的:

add_action('woocommerce_checkout_process', 'wh_wc_minimum_order_amount'); 

function wh_wc_minimum_order_amount() 
{ 
    $minimum = 7; 
    if (WC()->cart->get_cart_contents_count() % $minimum != 0) 
    { 
//  wc_clear_notices(); 
     wc_add_notice(sprintf('You must have an order with a minimum of %s to place your order.', $minimum), 'error'); 
    } 
} 

代碼放在functions.php文件的活躍兒童主題(或主題)的。或者也可以在任何插件php文件中使用。
代碼已經過測試和工作。

希望這會有所幫助!

+0

嗨!這很好,但我想在用戶點擊「下訂單」按鈕時做這項工作......當用戶下訂單時...... – Hateres1990

+0

@ Hateres1990:我已更新我的答案,請看看。 –

+0

我看到它的作品,但我需要這個版本。朋友,謝謝:) – Hateres1990