in Tutorials, WooCommerce

Adding a Section to a Settings Tab in WooCommerce

I published this over in the WooCommerce Developer Docs the other day. Please keep it in mind if you’re developing an extension for WooCommerce – along with the Create a WooCommerce Plugin guide.

When you’re adding building an extension for WooCommerce that requires settings of some kind, it’s important to ask yourself: Where do they belong?

If your extension just has a couple of simple settings, do you really need to create a new tab specifically for it? Most likely the answer is no.

When to Create a Section

So let’s say we had an extension that adds a slider to a single product page. This extension doesn’t have many options, just a couple:

  • Auto-insert into single product page (checkbox)
  • Slider Title (text field)

That’s only two options, specifically related to Products. We could quite easily just append them onto the core WooCommerce Products Settings (WooCommerce > Settings > Products), but that wouldn’t be very user friendly. Users wouldn’t know where to look initially so they’d have to scan all of the Products options and it would be difficult / impossible to link the options directly.

Fortunately, as of WooCommerce 2.2.2, there is a new filter in place that allows you add a new section, beneath one of the core settings’ tabs.

An example of a new section under the Products tab

An example of a new section under the Products tab

How to Create a Section

We’ll go over doing this through individual functions, but you should probably create a Class that stores all of your settings methods.

The first thing you need to is add the section, which can be done like this by hooking into the woocommerce_get_sections_products filter:

https://gist.github.com/bryceadams/cbc8ad536b01e4dd789a.js

Make sure you change the wcslider parts to suit your extension’s name / text-domain.

The important thing about the woocommerce_get_sections_products filter, is that the last part products, is the tab you’d like to add a section to. So if you want to add a new tab to accounts section, you would hook into the woocommerce_get_sections_accounts filter.

How to Add Settings to a Section

Now that you’ve got the tab, you need to filter the output of woocommerce_get_sections_products (or similar). You would add the settings like usual using the WooCommerce Settings API, but check for the current section before adding the settings to the tab’s settings array.

For example, let’s add the sample settings we discussed above to the new wcslider section we just created:

https://gist.github.com/bryceadams/7de0ef3652e15740af17.js

We’re hooking into the same woocommerce_get_sections_products filter, but this time doing a check that the $current_section matches our earlier defined custom section (wcslider), before adding in our new settings.

It will end up looking something like this:

Example of settings under a new section

Example of settings under a new section

Using the New Settings

You would now just use your newly created settings like you would any other WordPress / WooCommerce setting, through the get_option function and the defined ID of the setting.

For example, to use the previously created wcslider_auto_insert option, simply use the following code:

get_option( 'wcslider_auto_insert' )

Conclusion

When creating an extension for WooCommerce, think about where your settings belong before you create them. The key to building a useful product is making it easy to use for the end user, so appropriate setting placement is crucially important.

For more specific information on adding settings to WooCommerce, check out the Settings API documentation.

Write a Comment

Comment