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

前端 未结 5 1718
醉话见心
醉话见心 2021-01-06 02:42

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

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 03:05

    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 '
    '; // 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 '
    '; print_r($free_shipping_settings); echo '

    '; // 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.

提交回复
热议问题