Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How Can I Programatically Retrieve the Shipping Method from the WooCommerce Order?

WooCommerce offers various built-in shipping methods, such as flat rate, free shipping, and local pickup, and allows you to extend these options with plugins. Each shipping method is associated with a unique identifier and can have its own set of settings and costs. As a store owner or developer, you might need to programmatically retrieve the shipping method that the user has selected while placing an order for various purposes.

This guide explains how to programmatically retrieve the shipping method that was chosen by the user when placing the order on your WooCommerce store.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Solution: Programmatically Retrieve the Shipping Method from the WooCommerce Order

Imagine you run an online store that offers different shipping options to your customers, such as standard shipping, express shipping, and local pickup. You may need to programmatically retrieve and display the selected shipping method on the order listing admin page if you want to make some custom changes.

Below is the code snippet.

// Add a custom column for shipping method in WooCommerce orders
add_filter('manage_edit-shop_order_columns', 'ts_add_shipping_method_column');
function ts_add_shipping_method_column($columns) {
    $columns['shipping_method'] = __('Shipping Method', 'woocommerce');
    return $columns;
}

// Populate the shipping method column with data
add_action('manage_shop_order_posts_custom_column', 'ts_populate_shipping_method_column');
function ts_populate_shipping_method_column($column) {
    global $post;
    if ($column === 'shipping_method') {
        $order = wc_get_order($post->ID);
        $shipping_method = reset($order->get_shipping_methods());
        echo $shipping_method['name'];
    }
}

Output

The below output on the order listing page shows the Shipping Method Column, which displays Flat rates, and Free Shipping for all the orders in one place.

How Can I Programatically Retrieve the Shipping Method from the WooCommerce Order? - Tyche Softwares

Code Explanation

Here’s an explanation of the code:

  1. Adding the Custom Column
    • The manage_edit-shop_order_columns filter is used to modify the columns displayed in the WooCommerce orders list.
      • add_filter(‘manage_edit-shop_order_columns’, ‘ts_add_shipping_method_column’) hooks into this filter and calls the ts_add_shipping_method_column function when the orders list is being edited.
      • add_shipping_method_column($columns) is a callback function that receives the existing columns as an array ($columns) and adds a new column for “Shipping Method” to the array. It then returns the modified array of columns.
  2. Populating the Shipping Method Column
    • The manage_shop_order_posts_custom_column action is used to populate custom columns with data for each order row in the WooCommerce orders list.
      • add_action(‘manage_shop_order_posts_custom_column’, ‘ts_populate_shipping_method_column’) hooks into this action and calls the ts_populate_shipping_method_column function when WordPress is rendering the custom column content.
      • ts_populate_shipping_method_column($column) is a callback function that receives the column name being processed as $column. In this case, it checks if the column name is “shipping_method.”
      • Inside the ts_populate_shipping_method_column function, it checks if the current column being processed is the “shipping_method” column using $column === ‘shipping_method’.
      • If the column being processed is the “shipping_method” column, it retrieves the order data associated with the current row using wc_get_order($post->ID). Then, it extracts the shipping method information using reset($order->get_shipping_methods()). This is done because an order can have multiple shipping methods, so we take the first one.
      • Finally, it echoes the name of the shipping method, which will be displayed in the “Shipping Method” column for each order in the WooCommerce orders list.

Conclusion

This code snippet provides a way to add a custom column for the shipping method in the WooCommerce orders list within the WordPress admin panel. It involves adding a new column to the list of columns displayed for WooCommerce orders and populating that column with data.

Please provide feedback in the comments on the code’s effectiveness or if you need further clarification.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x