在WooCommerce中设置最低订购量

时间:2019-03-07 07:41:47

标签: php wordpress woocommerce cart checkout

我想在我的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' 
            );

        }
    }
}

2 个答案:

答案 0 :(得分:1)

function disable_checkout_button() { 

    // Set this variable to specify a minimum order value
    $minimum = 50;
    $total = WC()->cart->get_cart_subtotal();
    if( $total < $minimum ){
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        echo '<a style="pointer-events: none !important;" href="#" class="checkout-button button alt wc-forward">Proceed to checkout</a>';
    }  
}

add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button', 1 );

答案 1 :(得分:1)

要设置最低订购量,您可以使用woocommerce_check_cart_items操作挂钩:

add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

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

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

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

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

  

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


enter image description here


enter image description here


相关答案:Woocommerce set minimum order for a specific user role