Hello,

Sign up to join our community!

Welcome Back,

Please sign in to your account!

Forgot Password,

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

BashCow Latest Questions

  • 35
  • 35
rylan98

Minimum order amount except for specific shipping method in WooCommerce

In WooCommerce, I use the following code to set a minimum order amount: its very difficult for me to understand it and how to implement it. can someone guide

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

function wc_minimum_order_amount() {

    $minimum = 50; // Hier gibst du den Mindestbestellwert ein
    
    if ( WC()->cart->total < $minimum ) { 
        if( is_cart() ) { 
            wc_print_notice( sprintf( 'Der Mindestbestellwert beträgt %s pro Bestellung. Der aktuelle Bestellwert beträgt %s.' , // Text fuer Warenkorb 
                wc_price( $minimum ), 
                wc_price( WC()->cart->total )
            ), 'error' );
        } else {
            wc_add_notice( sprintf( 'Der Mindestbestellwert beträgt %s pro Bestellung. Der aktuelle Bestellwert beträgt %s.' , // Text fuer Kasse
                wc_price( $minimum ),
                wc_price( WC()->cart->total )
            ), 'error' );
        }
    }
}

Now if the customer chooses “self pickup” (“Local pickup shipping method), I don’t want any minimum required order amount.

How can I set Minimum order amount except for “Local pickup” shipping method in WooCommerce?

Related Questions

1 Answer

  1. This answer was edited.

    You can try this code, tested and it worked, it is for child theme. add_action( 'woocommerce_checkout_process', 'wc_minimum_required_order_amount' ); add_action( 'woocommerce_before_cart' , 'wc_minimum_required_order_amount' ); function wc_minimum_required_order_amount() { // HERE Your settings $minimum_amount = 100; // The minimum cart total amount $shipping_method_id = 'local_pickup'; // The targeted shipping method Id (exception) // Get some variables $cart_total = (float) WC()->cart->total; // Total cart amount $chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array) // Only when a shipping method has been chosen if ( ! empty($chosen_methods) ) { $chosen_method = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array) $chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id } // If "Local pickup" shipping method is chosen, exit (no minimun is required) if ( issRead more

    You can try this code, tested and it worked, it is for child theme.

    add_action( 'woocommerce_checkout_process', 'wc_minimum_required_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_required_order_amount' );
    function wc_minimum_required_order_amount() {
    
        // HERE Your settings
        $minimum_amount     = 100; // The minimum cart total amount
        $shipping_method_id = 'local_pickup'; // The targeted shipping method Id (exception)
    
        // Get some variables
        $cart_total     = (float) WC()->cart->total; // Total cart amount
        $chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)
    
        // Only when a shipping method has been chosen
        if ( ! empty($chosen_methods) ) {
            $chosen_method  = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
            $chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
        }
    
        // If "Local pickup" shipping method is chosen, exit (no minimun is required)
        if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
            return; // exit
        }
    
        // Add an error notice is cart total is less than the minimum required
        if ( $cart_total < $minimum_amount ) {
            $text_notice = sprintf(
                __("The minimum required order amount is %s (your current order amount is %s).", "woocommerce"), // Text message
                wc_price( $minimum_amount ),
                wc_price( $cart_total )
            );
            
            if ( is_cart() ) {
                wc_print_notice( $text_notice, 'error' );
            } else {
                wc_add_notice( $text_notice, 'error' );
            }
        }
    }
    See less
Leave an answer

Leave an answer