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

How to Show Shipping Method IDs in WooCommerce?

Shipping method IDs play a pivotal role in the flexibility and customization of your shipping options. The IDs are referred to by the WooCommerce plugin as well as all other 3rd party shipping plugins for doing any shipping related operations such as hiding WooCommerce shipping methods for certain conditionsshowing shipping costs on the WooCommerce product page & much more.

This post will guide you on how to show the shipping method IDs in WooCommerce.

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: Show Shipping Method IDs in WooCommerce

Below code snippet defines a function named “TS_ShowShippingMethodIDs“. This function shows a table that contains information about shipping zones, their corresponding shipping methods along with each method’s shipping ID on the shipping settings page of WooCommerce under WooCommerce → Settings → Shipping menu.

/*
 WooCommerce - Show shipping method IDs
*/

class TS_ShowShippingMethodIDs {
    // Returns an instance of this class. 
    public static function get_instance() {
        if (null == self::$instance) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    // Initialize the plugin variables.
    public function __construct() {
        $this->init();
    }

    // Set up WordPress specific actions.
    public function init() {
        // Print the table with Shipping zones, methods and their IDs.
        add_action('woocommerce_settings_shipping', array($this, 'ts_add_to_shipping_settings'), 20);
        // Add CSS to style the table.
        add_action('admin_head', array($this, 'ts_add_shipping_method_ids_css'));
    }

    // Return whether on the Shipping zones main page.
    private function ts_is_shipping_zones_page() {
        global $current_section;

        if (empty($current_section) && !isset($_GET['zone_id'])) {
            return true;
        }

        return false;
    }

    public function ts_add_shipping_method_ids_css() {
        if ($this->ts_is_shipping_zones_page()) {
            ?>
            <style>
                .shipping_method_ids { border: 1px solid #c3c4c7; }
                .shipping_method_ids td, .shipping_method_ids th { padding: 5px 10px; background-color: #fff; }
                .shipping_method_ids td { border-bottom: 2px solid #f9f9f9; }
                .shipping_method_ids ul { margin: 0; }
            </style>
            <?php
        }
    }

    // Print the table with Shipping zones, methods and their IDs.
    public function ts_add_to_shipping_settings() {
        if ($this->ts_is_shipping_zones_page()) {
            $data_store = WC_Data_Store::load('shipping-zone');
            $raw_zones = $data_store->get_zones();
            foreach ($raw_zones as $raw_zone) {
                $zones[] = new WC_Shipping_Zone($raw_zone);
            }
            ?>
            <h4>Shipping Method IDs</h4>
            <table class="shipping_method_ids">
                <tr>
                    <th>Zone name</th>
                    <th>Method name (Type): ID</th>
                </tr>
                <?php
                foreach ($zones as $zone) {
                    $zone_shipping_methods = $zone->get_shipping_methods();

                    if (count($zone_shipping_methods)) {
                        ?>
                        <tr>
                            <th valign="top"><?php echo $zone->get_zone_name() ?></th>
                            <td>
                                <ul>
                                    <?php
                                    foreach ($zone_shipping_methods as $index => $method) {
                                        $method_title = $method->get_method_title();
                                        $method_user_title = $method->get_title();
                                        $method_rate_id = $method->get_rate_id();

                                        printf(
                                            '<li>%s (%s): <strong>%s</strong></li>%s',
                                            $method_user_title,
                                            $method_title,
                                            $method_rate_id,
                                            "\n"
                                        );
                                    }
                                    ?>
                                </ul>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
            </table>
            <?php
        }
    }
}

$TS_ShowShippingMethodIDs = new TS_ShowShippingMethodIDs();

Output:

On the bottom of the shipping zone setting page, it shows the shipping method IDs as shown below.

How to Show Shipping Method IDs in WooCommerce? - Tyche Softwares

Code Explanation:

Let’s break down the code step by step:

  1. Class Definition (TS_ShowShippingMethodIDs):
    • This class encapsulates the functionality of the plugin.
    • It has a private static property instance to hold a singleton instance of the class.
    • The get_instance() method is a static method that returns an instance of the class, following the Singleton pattern.
  2. Constructor (__construct()):
    • The constructor initializes the plugin by calling the init() method.
  3. Initialization (init()):
    • This method sets up WordPress actions that hook into specific events.
    • add_action(‘woocommerce_settings_shipping‘, array($this, ‘ts_add_to_shipping_settings’), 20);: This action adds the plugin’s content to the WooCommerce shipping settings page.
    • add_action(‘admin_head’, array($this, ‘ts_add_shipping_method_ids_css’));: This action adds CSS styling for the table to the WordPress admin head section.
  4. Helper Method (ts_is_shipping_zones_page()):
    • This private method checks if the current page is the shipping zone main page in the WooCommerce settings.
  5. CSS Styling (ts_add_shipping_method_ids_css()):
    • This method adds CSS styles to the WordPress admin head section.
    • The styles define the appearance of the table that will display the shipping method IDs.
  6. Table Generation (ts_add_to_shipping_settings()):
    • This method generates the content to be displayed on the WooCommerce shipping settings page.
    • It first checks if the current page is the Shipping Zones main page using the ts_is_shipping_zones_page() method.
    • It then retrieves the shipping zones using WooCommerce’s data store.
    • It loops through each zone and extracts the shipping methods associated with the zone.
    • For each shipping method, it retrieves the method’s title, user-readable title, and rate ID.
    • The information is presented in a table format with zone names and corresponding shipping methods and their IDs.
  7. Instantiation and Execution:
    • The last lines of code create an instance of the Ts_ShowShippingMethodIDs class, which triggers the plugin functionality.

Conclusion:

This code snippet proves useful when you are planning out custom shipping options on your WooCommerce store via code. When using filters and hooks for customizations, these shipping IDs are what you would need.

Please share your feedback in the comments section regarding the usefulness of the code or if you have any further queries.

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

Share It:

Subscribe
Notify of
2 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Stanislav
9 months ago

Nice snippet, thanks!

Saranya
9 months ago
Reply to  Stanislav

You’re welcome. Thank you for your appreciation!

2
0
Would love your thoughts, please comment.x
()
x