在 WooCommerce 中设置最小订单金额

Kry*_*hey 3 php wordpress checkout cart woocommerce

我想在我的 WooCommerce 商店中设置最低订单金额。如果未达到金额但仍然可以结帐,以下代码完美地显示了通知。未达到最低金额时如何禁用结帐按钮?

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {
    // Set this variable to specify a minimum order value
    $minimum = 50;

    if ( WC()->cart->total < $minimum ) {

        if( is_cart() ) {

            wc_print_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        } else {

            wc_add_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 11

要设置最小订单金额,您可以通过woocommerce_check_cart_items以下方式使用操作挂钩:

add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {

    // HERE Set minimum cart total amount
    $minimum_amount = 250;

    // Total (before taxes and shipping charges)
    $cart_subtotal = WC()->cart->subtotal;

    // Add an error notice is cart total is less than the minimum required
    if( $cart_subtotal < $minimum_amount  ) {
        // Display an error message
        wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '<strong>', 'error' );
    }
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。

如果客户更新购物车更改数量或删除项目,行为也将更新。


在此处输入图片说明


在此处输入图片说明


相关答案:Woocommerce 为特定用户角色设置最低订单