在 WooCommerce 中添加最低订单价格,但仍允许客户在不满足条件时通过收取赤字来结帐

Add a minimum order price in WooCommerce, but still allow the customer to checkout when the condition is not met by charging the deficit

我想为 WooCommerce 添加最低订购价格。我找到了有关如何限制订购的说明,除非用户将更多产品添加到购物车以满足最低金额(例如 15 欧元)。

但是我希望这样,如果购物车中的产品总额少于 15 欧元,购物车总金额会自动增加到 15 欧元(+运费)。

这是我目前的情况:

add_action( 'woocommerce_before_cart' , 'total_order_amount' );
add_action( 'woocommerce_review_order_before_order_total' , 'total_order_amount' );
//include the minimum total in the total price in Cart and Checkout pages 
function total_order_amount( ) { 
    // Set this variable to specify a minimum order value
    $minimum = get_option( 'cart_minimum_total' );
    if ( WC()->cart->subtotal < $minimum ) {
        if( is_cart() || is_checkout() ) {
            $new_price = (WC()->cart->total - WC()->cart->subtotal) + $minimum;
            WC()->cart->total = $new_price;
        } 
    }
}
 
add_action( 'woocommerce_review_order_after_order_total', 'show_minimum_total');
add_action( 'woocommerce_cart_totals_after_order_total', 'show_minimum_total');
//Show the minimum total included in the final price
function show_minimum_total() {
    if ( WC()->cart->subtotal < get_option( 'cart_minimum_total') ) {
        echo '<tr class="cart_minimum_total">';
        echo '<th>'.esc_html__( 'Includes minimum price', 'myplugin' ).'</th>';
        echo '<td data-title="'.esc_attr__( 'Includes minimum price', 'myplugin' ).'">'.get_option( 'cart_minimum_total' ).' €</td>';
        echo '</tr>';
    }
}

另外还有管理部分,管理员可以在其中将 cart_minimum_total 设置为 15 欧元,但这工作正常。

我的问题是,我不知道如何将总和包含在 cart/purchase 中,当用户点击“发送订单”时,总和也会包含在订单中。

我假设最低金额应该与运费类似“item/product”。

为什么不简单地加一笔费用呢?这样,客户将看到需要添加的金额才能满足最低要求。费用也显示在所有订单概览(前端和后端)的单独行中,在 e-mail 通知中,并自动包含在订单总额中

所以你得到:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Set this variable to specify a minimum order value
    //$minimum = get_option( 'cart_minimum_total' );
    $minimum = 15;

    /* ------------------------ */

    // Get subtotal
    $subtotal = $cart->subtotal;

    // OR USE

    // Get subtotal
    $subtotal = 0;

    foreach ( $cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['line_subtotal'];
        $subtotal += $cart_item['line_subtotal_tax']; // with taxes
    }

    /* ------------------------ */

    if ( $subtotal < $minimum ) {
        // Calculate
        $fee = $minimum - $subtotal;

        // Applying
        $cart->add_fee( __( 'Includes minimum price', 'woocommerce' ), $fee, true, '' );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );