Your Web News in One Place
October 9, 2019 01:43 pm

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 wewill setup the required shortcodes. Should you wish to take your WooCommerce product archive further, you could convert it to a Gutenberg block,use it with Elementor as a widget, or integrate it with Visual Composer.

This 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 afolder called (perhaps not the most original name) “woocommerce-products”, then inside that foldercreate 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

For our WooCommerce product archive plugin we will next 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 imageLoadedplugin (which comes with the WordPress core), and to create the carousel we will use the wildly popularjQuery 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 WordPressplugin 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 thefirst parent container add this code to the PHP file:

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

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

This shortcode has two attributes; the title and the activeindicator, 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, youwould see unstyled and non-functional HTML output. So let’s fix it. 

3. Add jQuery to Power the Tabbed Content

Go to themain.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 orjQuery, 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 somebasic CSS:

4. Create WooCommerce Products Shortcode

We’re making great progress; our tabbed WooCommerce product archive is nearly there! The time has come tocreate 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 canbe grid or carousel
  • Autoplaytrue or false, this is an additional option for the carousel layout
  • Columns – here you canspecify how many columns you want
  • Rows – this is the sameas columns, and only applies to the carousel layout
  • Quantity – the number of items to display
  • Type – here youcan 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 rowsper 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 havethe question: do you do this with JavaScript or with PHP? I prefer the second option, as JS hassome 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 toloop through our query and wrap each two, each three, and each four product itemsinside the carousel item wrapper markup.

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

This happens right after the code:

5. Add Owl Styles 🦉

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

Open the style.css fileand paste the owl carousel styles (you should find these in your owlCarouselplugin download .zip file). And some additional style for carousel navigationand our products:

But wait a minute, we haven’t yet called the carousel plugin, solet’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 surethat our images are loaded to avoid any kind of content overlapping, and thenwe call the plugin for our carousel products layout.

6. Fix Carousel Glitch

One thing remains to be done: if you now go to thetabs and state that you want a carousel based layout you will see it working on the active tabfor the first page load, but if you start switching from tab to tab yourcarousel 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 ischanged. And now inside tab function right after the code:

call the function:

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

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

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


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

Original Link:

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code