5

I have did tried to use the code from this answer:
How to get minimum order amount for free shipping in woocommerce

But it return a NULL result and I can't find the way to fix this code until now.

How can I get the right minimun order amount on checkout page?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
huykon225
  • 563
  • 5
  • 17
  • Possible duplicate of [How to get minimum order amount for free shipping in woocommerce](http://stackoverflow.com/questions/26582039/how-to-get-minimum-order-amount-for-free-shipping-in-woocommerce) – Marek Urbanowicz Feb 13 '17 at 09:55
  • please read again my description. I have do like that (your link) but It return NULL result. I am doing like this: $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping = $chosen_methods[0]; but how to I can get min_amount if chosen method is Free Shipping ? – huykon225 Feb 13 '17 at 10:02
  • @LoicTheAztec you mean I have to change the question or description ? all things I want : when I come checkout page and chose a shipping method is Free Shipping, I want to get **minimum amount** of this method. can you help me ? – huykon225 Feb 13 '17 at 10:40
  • @LoicTheAztec you didn't understand what I want. I want when I go **checkout page** I want to get value **minimum order amount** of **Free Shipping** method. I was do like this: `$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping = $chosen_methods[0]; print_r($chosen_shipping);` but I just received the name of method (eg: free_shipping:14) . how can I get minimum amount of that ? – huykon225 Feb 13 '17 at 13:12
  • oh. thank you :(( do you know anyone can help me ? – huykon225 Feb 13 '17 at 14:18

5 Answers5

9

The code of this answer: How to get minimum order amount for free shipping in woocommerce
is obsolete with WooCommerce version 2.6+, but it was helpful for this functional answer…

After making some search and some tries, I have found the way to get the minimum Order amount that is set in the Free Shipping method, for a specific Zone (Region):

enter image description here

Here is the working tested code (explanations are commented inside):

// Here you get (as you already know) the used shipping method reference
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );

// Replacing inside the string ':' by '_'
$option_value = str_replace(':', '_', $chosen_methods[0]);

// We concatenate the string with additional sub-strings
$option_value = 'woocommerce_'.$option_value.'_settings';

// Just a test => outputting the string to see what we get
echo $option_value; echo '<br>';

// Now we can get the options values with that formatted string
$free_shipping_settings = get_option( $option_value );

// Just for test => we output the (pre-formatted) array of values to check
echo '<pre>'; print_r($free_shipping_settings); echo '</pre><br>'; 

// Here we get the value of the order min amount (Finally!)
$order_min_amount = $free_shipping_settings['min_amount'];

// We output the value (to check)
echo 'Order min amount: '.$order_min_amount;

Bingo! you get it.

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    thank you so much. I have just found it and go here same the result. thank you again ! – huykon225 Feb 13 '17 at 15:37
  • 2
    When a customer has not reached the minimum amount yet and I use `WC()->session->get( 'chosen_shipping_methods' );`, it returns the current shipping method (the non-free) instead. – Michael Walter Sep 24 '20 at 10:06
4

Right way for get this..

function get_free_shipping_minimum($zone_name = 'England') {
    if ( ! isset( $zone_name ) ) return null;

    $result = null;
    $zone = null;

    $zones = WC_Shipping_Zones::get_zones();
    foreach ( $zones as $z ) {
        if ( $z['zone_name'] == $zone_name ) {
            $zone = $z;
        }
    }

    if ( $zone ) {
        $shipping_methods_nl = $zone['shipping_methods'];
        $free_shipping_method = null;
        foreach ( $shipping_methods_nl as $method ) {
            if ( $method->id == 'free_shipping' ) {
                $free_shipping_method = $method;
                break;
            }
        }

        if ( $free_shipping_method ) {
            $result = $free_shipping_method->min_amount;
        }
    }

    return $result;
}
0

You need to get the option by getting ,

get_option( 'woocommerce_free_shipping_1_settings' ); 

And then unserialize the data by doing ,

maybe_unserialize();
Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
  • thank your answer. my db have too much **woocommerce_free_shipping_x_settings** (with x: 9->19). I want get min amount Free Shipping method in checkout page. do you understand the question ? please help me – huykon225 Feb 13 '17 at 10:18
0

These 2 filters allow you to get the minimum order amount or even to change its value:

  • woocommerce_shipping_free_shipping_instance_option
  • woocommerce_shipping_free_shipping_option

You could use it like this for example:

add_filter( 'woocommerce_shipping_free_shipping_instance_option', 'get_free_shipping_min_amount', 10, 3 );
add_filter( 'woocommerce_shipping_free_shipping_option', 'get_free_shipping_min_amount', 10, 3 );
function get_free_shipping_min_amount( $option, $key, $method ){
    if (
        'min_amount' !== $key ||
        ! is_numeric( $option )     
    ) {
        return $option;
    }
    // Minimum amount
    $min_amount = $option;
    return $option;
}

You can get more info from the shipping method using the $method param like instance_id or something like that if you wish.

You can also replace 'free_shipping' from the hook to get info from other shipping methods. The hook works like this: 'woocommerce_shipping_' . $shipping_method_id . '_option'.

You can take a look at it on the documentation:

Pablo S G Pacheco
  • 2,550
  • 28
  • 28
-1

Maybe it will help someone. You can create new free shipping object and get from it min_amount. I think it's simpler than in @LoicTheAztec answer.

if ( class_exists( 'WC_Shipping_Free_Shipping' ) ) {
    $free_shipping = new WC_Shipping_Free_Shipping();
    $min_amount = $free_shipping->min_amount;
    echo $min_amount;
}
Alex
  • 1