1

How can I retrieve the shipping methods in my WooCommerce shop? I have this on my checkout page:

my checkout page

And inside my admin it looks like this:

shipping methods in wp admin

I have tried doing:

WC()->shipping->load_shipping_methods()

But it does not return GLS or Click and collect text / price. It returns this data: https://paste.laravel.io/0c1aff96-dbed-452c-856f-6d62b6f1f47e

3
  • What output do you get when using WC()->shipping->get_shipping_methods(); ? – 7uc1f3r Feb 16 '20 at 8:37
  • @L7c1f3r the same output as in the pastebin.. – FooBar Feb 16 '20 at 9:53
  • I have now tested this and indeed, the same result. If I look at your output and compare it to mine, I see that the data is present, but for example 'GLS' is missing as method_title, but 'Flat rate' is shown there, but this is the same with my output, so there's nothing wrong with that. 'GLS', turns out to be the label. What is your ultimate intention, what data do you want to retrieve and where do you want to use it afterwards? – 7uc1f3r Feb 16 '20 at 20:27
2

You need to use something different as following (commented):

// Loop through shipping packages from WC_Session (They can be multiple in some cases)
foreach ( WC()->cart->get_shipping_packages() as $package_id => $package ) {
    // Check if a shipping for the current package exist
    if ( WC()->session->__isset( 'shipping_for_package_'.$package_id ) ) {
        // Loop through shipping rates for the current package
        foreach ( WC()->session->get( 'shipping_for_package_'.$package_id )['rates'] as $shipping_rate_id => $shipping_rate ) {
            $rate_id     = $shipping_rate->get_id(); // same thing that $shipping_rate_id variable (combination of the shipping method and instance ID)
            $method_id   = $shipping_rate->get_method_id(); // The shipping method slug
            $instance_id = $shipping_rate->get_instance_id(); // The instance ID
            $label_name  = $shipping_rate->get_label(); // The label name of the method
            $cost        = $shipping_rate->get_cost(); // The cost without tax
            $tax_cost    = $shipping_rate->get_shipping_tax(); // The tax cost
            $taxes       = $shipping_rate->get_taxes(); // The taxes details (array)
        }
    }
}

Now if customer session is not yet enabled (customer hasn't add to cart any product yet), you need to force WooCommerce customer session activation using the following:

// Enable customer WC_Session
add_action( 'init', 'wc_session_enabler' );
function wc_session_enabler() {
    if ( is_user_logged_in() || is_admin() )
        return;

    if ( isset(WC()->session) && ! WC()->session->has_session() ) {
        WC()->session->set_customer_session_cookie( true );
    }
}

Code goes in functions.php file of your active child theme (or active theme).

If needed, you can also get the customer chosen shipping method(s) with the following:

$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
3
  • Do you know if WC()->session->get( 'chosen_shipping_methods' ); still works if there is only one shipping method available? – Sam Assoum Jan 25 at 18:16
  • 1
    @SamAssoum Yes it does work … – LoicTheAztec Jan 25 at 20:05
  • I want to display the selected shipping method to the My Order section which I made custom, So how can I print the selected shipping method in session print to My Order section? – Ravi Mar 7 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.