Search code examples
wordpressfunctionwoocommerceminimum

Problem with wordpress + woocommerce and minimum order but except local pickup


Found a code for my exact problem and used it , while it works perfect for minimum order for some reason it does not work for when choosing local pickup. my current shipping setup:

my current shipping setup

and the website is papabross.gr

I used this code:

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

// HERE Your settings
$minimum_amount     = 25; // The minimum cart total amount
$shipping_method_id = 'local_pickup:10'; // 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 ) {
    wc_add_notice( sprintf(
        __("Η ελάχιστη παραγγελία για αποστολή είναι %s (Η παραγγελία σας μέχρι στιγμής είναι %s).", "woocommerce"), // Text message
        wc_price( $minimum_amount ),
        wc_price( $cart_total )
    ), 'error' );
}
}

Not really sure if I am choosing the correct shipping id? Can I use another hook maybe?

I have used a working code I found here and everything works but the local pickup. It still asks for a minimum order.

I wonder if I have used the shipping id wrongly?


Solution

  • After a long search, I found a code that works! I am sharing this in case someone gets the same problem:

    // Set a minimum amount of order based on shipping zone & shipping method before checking out.
    add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );
    
    // Only run in the Cart or Checkout pages.
    function cw_min_num_products() {
        if ( is_cart() || is_checkout() ) {
            global $woocommerce;
    
            // Set the minimum order amount, shipping zone & shipping method before checking out.
            $minimum         = 25;
            $county          = array( 'GR' );
            $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
            $chosen_shipping = explode( ':', $chosen_shipping );
    
            // Defining var total amount.
            $cart_tot_order = WC()->cart->subtotal;
    
            // Compare values and add an error in Cart's total amount.
            // happens to be less than the minimum required before checking out.
            // Will display a message along the lines.
            if ( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && $chosen_shipping[0] != 'local_pickup' ) {
                // Display error message.
                wc_add_notice(
                    sprintf(
                        'Δεν έχετε φτάσει ακόμη το ελάχιστο ποσό παραγγελίας των %s€.' . '<br/>Δεν υπάρχει ελάχιστη παραγγελία εάν επιλέξετε την παραλαβή από το κατάστημα.'
                        . '<br />Το τρέχον ποσό της παραγγελίας σας είναι : %s€.',
                        $minimum,
                        $cart_tot_order
                    ),
                    'error'
                );
            }
        }
    }