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

But我希望这样,如果购物车中的产品总数少于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>';
    }
}

此外还有管理部分,管理员可以将购物车的最低总金额设置为15欧元,但这很好.

我的问题是,我不知道如何在购物车/购物中包含金额,当用户单击"发送订单"时,它也会包含在订单中.

我假设最低金额应该是与装运类似的"项目/产品".

推荐答案

为什么不加一笔费用呢?这样,客户将看到需要添加的金额,以满足最低要求.费用还显示在所有订单概述(前端和后端)的单独一行上,并显示在邮箱通知中,并自动包含在订单总额中

所以你得到:

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;

    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 );

Php相关问答推荐

尽管包含目标日期,但日期范围比较仍有意外输出

在特定日期之间使用特定的元密钥销售产品

中继器内置中继器的ACF WordPress组

在WooCommerce中设置带有国家/地区和年份前缀的顺序序号

Symfony 6.2 IsGraned具有多个角色

如何在UML类图中可视化变量引用数组

在PHP中处理Linux输入事件(/dev/input/Event*)

模式 bootstrap 未显示

调用未定义的方法 mysqli::execute_query()

如何显示所有数组数据而不仅仅是最后的结果

尽管有use指令,PHP 类在其他文件中仍处于可见状态

Laravel Eloquent(where 子句)

通过存储库检索 Blade 上的单值数据 出错

后退按钮不起作用 laravel ajax crud

PHP文件如何从CSS执行

PHP header() 是否缓存重定向,如果是,如何防止它这样做?

Laravel 测试 assertJsonMissing 不适用于唯一的键.为什么?

PHP 中的 curl 验证失败(openssh 连接正常)

我想更新表格中的结果并将它们发布到浏览器,但它不起作用

升级到 PHP 8.2:ksort 和 krsort 更改