woocommerce 中的产品最小数量乘以 7

Product minim amount multiply by 7 in 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 ) );
    }
}

已更新

You need to hook your function into woocommerce_checkout_process, to do your calculation and display the error.

像这样:

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 文件中。
代码已经过测试并且可以工作。

希望对您有所帮助!