Getting minimum order amount for 'Free Shipping' method in checkout page

落花浮王杯 提交于 2019-12-07 06:30:52

问题


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?

Thanks


回答1:


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):

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.




回答2:


You need to get the option by getting ,

get_option( 'woocommerce_free_shipping_1_settings' ); 

And then unserialize the data by doing ,

maybe_unserialize();



回答3:


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;
}



回答4:


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;
}


来源:https://stackoverflow.com/questions/42201045/getting-minimum-order-amount-for-free-shipping-method-in-checkout-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!