Wednesday, October 9, 2019

Build a Tabbed Product Archive for Your WooCommerce Store

Build a Tabbed Product Archive for Your WooCommerce Store

In this tutorial you will learn how to make your WooCommerce store a little more stylish by organizing the product archive with tabs. We’ll create tabs with a multi-column layout, a multi-row carousel, and a grid layout.

What We’re Going to Build

Over the coming steps we will create a WordPress plugin, inside which we will setup the required shortcodes. Should you wish to take it further, you could convert it to a Gutenberg block, use it with Elementor as a widget, or integrate it with Visual Composer.

Our tabbed product archive will be perfect for showing recent, featured, sale, and best selling products, like this:

tabbed product archive in woocommerce
Our product archive, showing tabs and carousel controls

So let’s get started!

1. Create a WordPress Plugin

In your WordPress site under wp-content/plugins create a folder called (perhaps not the most original name) “woocommerce-products”, then inside that folder create a php file with the same name. Open the file and paste into it the code below (change the details to fit your own):

Here we’ve done nothing more than define our plugin name and provided some meta info.

Enqueue Scripts and Styles

Next we will need to enqueue the required scripts and styles, so add the following code snippet:

First, we’ve added a basic CSS file to store our styles. Next we loaded the built-in imageLoaded plugin (which comes with the WordPress core), and to create the carousel we will use the wildly popular jQuery plugin owlCarousel. And, of course, the main js file. 

Note: Make sure these files are added to your plugin folder in the relevant subfolders. 

For more on WordPress plugin and shortcode creation, or further information on using owl.js, here are some recommended tutorials on Tuts+:

2. Create a Tab Shortcode

In this step we will create a tab shortcode. It will consist of two shortcodes actually; the parent tab container, and then child tab items. For the first parent container add this code to the PHP file:

This shortcode doesn’t require any attributes, it just wraps the tab items inside it.

Speaking of tab items, next add the code below to create the child tab item element:

This shortcode has two attributes; the title and the active indicator, in case you want to make the targeted tab active on first load. 

When used in the WordPress editor the two shortcodes together will look like this:

At this point, if you were to add them to your front-page, then view the result in your browser, you would see unstyled and non-functional HTML output. So let’s fix it. 

3. Add jQuery to Power the Tabbed Content

Go to the main.js file and paste the code

I won’t describe all this code in great detail, but the logic here powers the tab functionality. If you are not familiar with JavaScript or jQuery, I highly recommend this amazing free course for beginners:

Style the Tabs

Now the tabs work, but they still appear ugly, so let’s add some basic CSS:

4. Create WooCommerce Products Shortcode

We’re making great progress! The time has come to create a shortcode for the products. This will fetch the products with a query, then from those results create a series of arrays: featured, sale, and best-selling products. It will then output the contents of those arrays to give us our tabs of products.

Add this code to the PHP file:

The shortcode we’ve created here has several attributes:

  • Layout – this can be grid or carousel
  • Autoplaytrue or false, this is an additional option for the carousel layout
  • Columns – here you can specify how many columns you want
  • Rows – this is the same as columns, and only applies to the carousel layout
  • Quantity – the number of items to display
  • Type – here you can specify recent products, best sellers, sale, or featured products

A Note on Our Carousel Rows

When you have a carousel, each product becomes a carousel item–normal behavior when you have one row. However, what happens if you want two, three, or four rows per carousel item? 

In this case you would need to wrap each of these possibilities inside a wrapper that would act as the carousel item. And you have the question: do you do this with JavaScript or with PHP? I prefer the second option, as JS has some delay on execution. So if you examine the shortcode you will find the following:

Note the

right after the

This is very similar to a for loop, we need an increment to loop through our query and wrap each two, each three, and each four product items inside the carousel item wrapper markup.

You’ll notice the %, which is the modulo operator. It gives the remainder of a division; 0 when the number is exactly divisible by 2/3/4, and 1 when not. And with each loop we will need to increment the counter with:

This happens right after the code:

5. Add Owl Styles 🦉

We’re almost ready, but first let’s add some owl carousel styles and some additional styling to our products. 

Open the style.css file and paste the owl carousel styles (you should find these in your owlCarousel plugin download .zip file). And some additional style for carousel navigation and our products:

But wait a minute, we haven’t yet called the carousel plugin, so let’s now go to the main.js file, and right after the tab code add the following:

Here we use two plugins at the same time: first we make sure that our images are loaded to avoid any kind of content overlapping, and then we call the plugin for our carousel products layout.

6. Fix Carousel Glitch

One thing remains to be done: if you now go to the tabs and state that you want a carousel based layout you will see it working on the active tab for the first page load, but if you start switching from tab to tab your carousel layout will break. On the tab change event our carousel needs to updated. So at the very beginning of the file add the function:

This function refreshes the carousel layout each time the tab is changed. And now inside tab function right after the code:

call the function:

Now, clear the browser cache and add the following shortcode content to your page:

In this case I am using the default WordPress theme with Gutenberg editor (hence the Gutenberg comments <!-- wp:separator -->), if you have the classic editor, just copy the shortcodes.

And now if you have done everything right you will see a beautiful layout like this!

Conclusion

You are free to use and modify this plugin in your projects, both commercial and non-commercial. Visit the repo on Github to grab the source code in full. I hope you find it useful, thanks for reading!

Best WooCommerce Themes on Envato Market


No comments:

Post a Comment