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

How to Add a Section to the WooCommerce Settings Under the “Shipping” Tab?

When creating Plugins with WooCommerce, you can either have the plugin’s settings in their own sub-menu, or you can have them within the WooCommerce menu itself on a separate page or under the ‘Settings’ page. That’s where the WooCommerce Settings API comes in. You can define your settings right in the WooCommerce settings page itself. This will make it easier for your customers to manage all settings from one place.

You can create sections for your settings in WooCommerce by using the filter woocommerce_get_sections_{tab_name}. For instance, if you want to create a section in the Shipping tab, you can use woocommerce_get_sections_shipping.

In this blog post, we will discuss how to add a new section to the “Shipping” tab in the settings of your WooCommerce store in order to customize its functionality.

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: Adding a New Section to the WooCommerce Settings “Shipping” Tab

Let’s say you want to add a new section named “Shipping Coupons” in the WooCommerce → Settings → Shipping page.

For such an instance, you can create a new section by using the below code snippet for adding a section to the WooCommerce settings under the “Shipping” tab.

if ( ! class_exists('WC_Shipping_Coupon')){
    class WC_Shipping_Coupon{
        public function __construct(){
            add_filter('woocommerce_get_sections_shipping', array( $this, 'ts_add_shipping_settings_section_tab') );
        }

        public function ts_add_shipping_settings_section_tab( $section ){
            $section['shipping_coupon'] = __('Shipping Coupons', 'shipping-coupon');

            return $section;
        }
    }
    $GLOBAL['wc_shipping_coupon'] = new WC_Shipping_Coupon();
}

Output

We define our shipping coupon settings section, called shipping_coupon, by adding it to the $settings_tab array and it will display the name Shipping Coupons as shown below.

How to Add a Section to the WooCommerce Settings Under the "Shipping" Tab

In the below scenario, clicking on the “Shipping Coupons” tab redirects you to the corresponding page.

How to Add a Section to the WooCommerce Settings Under the "Shipping" Tab

Code Explanation

Here’s the breakdown of the code snippet

1. Class Check

This line checks whether the class WC_Shipping_Coupon does not exist. If it doesn’t exist, it will proceed to create this class and its associated functionality.

2. Class Declaration

Here, a new PHP class called WC_Shipping_Coupon is being defined.

3. Constructor

This is the constructor method for the WC_Shipping_Coupon class. It is called when an object of this class is created. Inside the constructor, there is an action hook being added using add_filter.

4. Adding Filter Hook

This line adds a filter hook to the ‘woocommerce_get_sections_shipping‘ filter. When this filter is triggered, it will call the ts_add_shipping_settings_section_tab method of the current instance of the WC_Shipping_Coupon class ($this). This filter is typically used in WooCommerce to add sections to the shipping settings in the WordPress admin.

5. Section Addition Method

This is the ts_add_shipping_settings_section_tab method within the WC_Shipping_Coupon class. It receives a parameter $section which is an array representing sections in the shipping settings.

6. Adding New Section

Within the ts_add_shipping_settings_section_tab method, a new section is added to the $section array. This section is given the key ‘shipping_coupon’ and the label ‘Shipping Coupons’. The __(‘Shipping Coupon’, ‘shipping-coupon’) function is used to translate the label, and the ‘shipping coupon’ is a text domain for localization.

7. Class Instance Creation

This line creates a new instance of the WC_Shipping_Coupon class and stores it in the global variable $GLOBAL[‘wc_shipping_coupon’].

Conclusion

Once the new section is added, you can also add the custom fields for the new section in the WooCommerce settings page.

Please share your feedback in the comments regarding the code’s usefulness or if you have further questions.

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