WooCommerce: минимальная сумма для покупки


Итак, задача была следующая -ввести минимальную сумму в корзине для доставки "Flat rate", т.е. если человек не набрал до 40$ и выше и при этом выбрал доставку курьером - ему выводится сообщение и отключается возможность оформить заказ. Следующий код можно добавить в function.php от шаблона или свой кастомный плагин:

/**
 * Function to set a minimum/cap on the shipping rates.
 */
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
do_action( 'woocommerce_shipping_method_chosen', 'wc_minimum_order_amount' );
 
function wc_minimum_order_amount() {
    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
	
    $minimum = 40;
    if ( WC()->cart->total < $minimum && $chosen_methods[0] == 'flat_rate:2' ) {

        if( is_cart() ) {

            wc_print_notice( 
                // %s is the minimum order amount for deliveries with the <strong>Flat Rate</strong>. Your current order total amount is %s.
				sprintf( 'Deliveries require a minimum order of %s. Your current order total is %s.' , 
                    wc_price( $minimum ), 
                    wc_price( WC()->cart->total )
                ), 'error' 
            );

        } else {

            wc_add_notice( 
                sprintf( 'Deliveries require a minimum order of %s. Your current order total is %s.' , 
                    wc_price( $minimum ), 
                    wc_price( WC()->cart->total )
                ), 'error' 
            );

        }
    }

}

на всякий случай через var_dump() уточните что лежит у вас в переменной $chosen_methods[0]