How to Get Current Shipping Method WooCommerce

Get Current Shipping Method WooCommerceIn this post, I want to show you how to get current shipping method in WooCommerce.

Using this get current shipping method WooCommerce code snippet I will share in this post, I will illustrate how you can get the shipping method.

Why Get Current Shipping Method WooCommerce?

If you are developing a custom WooCommerce shipping plugin, you are most likely going to look for a way to get the current shipping method in WooCommerce.

You can also get the current shipping method when you want to create custom functionality.

For example, in my recent plugin development, I wanted to get the current shipping method so that I would create WooCommerce redirect after checkout based on the shipping method.

In this post – how to set up WooCommerce redirect after checkout, I outlined how you can set up the redirect after checkout based on the shipping method.

I also shared my plugin that can help you create conditional WooCommerce redirects after payment.

So this is one of the many reasons you may want to get the current shipping method.  So let us now see how to get this done.

How to Add WooCommerce Shipping Method

Before you get started with coding the shipping methods, it helps to first understand how to set up the shipping methods. The following are quick steps on how to set up a shipping method in WooCommerce:

  1. Login into your WooCommerce dashboard and go to Settings > Shipping Options and Click on the Add Shipping Zone as shown in the image below:WooCommerce Add Shipping Zone
  2. The second step is to fill in the name of the zone and select the regions then click on the button to add the shipping method as shown on the image below :Add Shipping Zone
  3. The third step is to select the shipping method from the dropdown and then click the Add Shipping method button as shown on the image below :WooCommerce Add Shipping Zone
  4. Finally, you should see that the WooCommerce shipping method has been added and it is enabled as shown in the image below:WooCommerce Add Shipping Zone

Now let us go ahead and see how to get the current shipping method WooCommerce.

Get Current Shipping Method WooCommerce

To get the current shipping method in the front end, you need to understand how the user selects the shipping method.

The customer selects the shipping method on the cart page as shown in the image below:

Select Shipping Method

It is important to note that the option to select a shipping method in WooCommerce will only appear when you have more than one shipping method added to the settings page as shown in the image below:

Shipping Method

Now we can go back to the code and find out how to get the current shipping method in WooCommerce.

Get Current Shipping Method WooCommerce and Display in Header

To get the current shipping method, we need to get the WooCommerce object session of the option that the user has selected on the cart page as shown in the image above.

To get this session, we need to access the WooCommerce object – WC ().

What is WC () function?

This is simply a function that returns the instance of the WooCommerce and you can now go ahead to access the methods and the properties in the class as you will see shortly in my code example.

For example, you can use this function as follows:


WC()->session->get('foo');


To get the current shipping method in WooCommerce we can go ahead and use the function and the get method as follows:

$current_shipping_method = WC()->session->get( ‘chosen_shipping_methods’ );

We can now go ahead and add this to the WordPress header hook for illustration purposes and you can have it in the action hook as in the code below:


// Get Current Shipping Method WooCommerce

add_action('wp_head', 'get_current_shipping_method');

function get_current_shipping_method(){

$current_shipping_method = WC()->session->get( 'chosen_shipping_methods' );

print('<pre>');

print_r( $current_shipping_method );

print('</pre>');

}


When you add this code to the functions.php file of your theme, you can test it as I have added it to the customized Storefront theme as you can see in the image below.

Current Shipping Method

When I proceed to the checkout page, I see the current shipping method displayed in the header:

Conclusion 

In this post, I have illustrated and shared the get current shipping method WooCommerce code snippet that you can easily implement in your custom WooCommerce plugin or WooCommerce theme development.

Similar Articles