Установить минимальную сумму заказа в WooCommerce - PullRequest
1 голос
/ 07 марта 2019

Я хочу иметь минимальную сумму заказа в моем магазине 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 ]

1 голос
/ 07 марта 2019

Чтобы установить минимальную сумму заказа, вы можете использовать woocommerce_check_cart_items action hook следующим образом:

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 устанавливает минимальный заказ для определенной роли пользователя

1 голос
/ 07 марта 2019
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 );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...