Recently I was asked to help out a local shop to develop a new WordPress site with WooCommerce. This is normally a super simple, straightforward task, but this one had a catch (don’t they always?).
The customer had a requirement for “Team Stores” wherein they make custom products for Teams (ie local high school) where teams have products that are not displayed in the regular shop. This means they need to be able to add teams, show a list of all teams with logos, and then have shops that only display team products.
Approach – WooCommerce Extension
To approach this requirement I decided to develop a WooCommerce Extension plugin for a few reasons but mainly so that the “Product Teams” features would be portable to any theme that the client chooses now and also in the future, ie the client won’t be locked into one highly custom theme that has their business logic in it. It’s always much better to separate business logic from display wherever possible and this is no exception.
Intro WooCommerce Product Teams
The plugin solution involves several different pieces including a new ‘teams’ taxonomy for WooCommerce Products’ custom post type, custom taxonomy meta for the new teams taxonomy to support Team Logos, and overriding the default products query to filter out team products. The most interesting part of this plugin is registering a new taxonomy for WooCommerce Products and then overriding the product’s query without editing a theme, let’s see how these can be done.
Register a New Taxonomy
The first thing we’ll need to do is register a new taxonomy for the product Teams.
Excluding Team Products from the Shop
The next semi-challenging thing I needed to do was filter out the “Team Products” from the regular shop without editing the WooCommerce template files or adding any new templates to the theme. To do this, the pre_get_posts action perfectly fits the task to help us override the products query. Note – pre_get_posts overrides all queries on the frontend and backend, so you’ll need to be careful and use some logic on when and where you use this.
Here is the full function that allows us to override the query only on the frontend and also only on the shop index, category and tag archive pages.
The end result is that I was able to register the teams taxonomy and control how they are displayed on the frontend in a theme agnostic manner. Here is a link to the full plugin on github – WooCommerce Product Teams. Enjoy!