2017-05-09 98 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