Global Product Categories Solved! A tutorial

With some help from other users and WPMU DEV staff, I've able to successfully complete control over product categories on a multisite network. I'm posting this in the hopes so that Aaron can mark it off his list of things to do. If it can be done using a workaround like this, then I think he shouldn't devote his time to coding it in. This work around is easy and has multiple benefits.

There are two parts to this. The first section covers new multisite setups (or setups with no existing subsite stores/products). The first section will describe to ensure that all new stores/sites will have your declared product categories embedded in them the moment they are created.It also describes how to prevent any new product categories from being added by sellers. The second section will describe a method to clean up the mess created by existing stores with their own categories and tags.

PART 1: BASIC SETUP

~ Step 1. Decide what your categories are going to be ~
This is important, make a list and keep it handy, if you have to do Part 2 of this instruction, you'll entering this list of categories in two places

~ Step 2. Create a New Blog Template ~
For multisite operators, this should be an essential thing to do. It allows you to brand any new sites on your network, as well as set them up in a way that is compatible with how you envision your network.

Use the WPMU Plugin for this: https://premium.wpmudev.org/project/new-blog-template

When setting up the template, activate the Marketpress plugin, then manually add all of the product categories you decided on in step 1. This will add them to the database for that blog. We use MP as a supporter plugin, if you do that, its okay to deactivate MP once the product categories have been added. They will still be there and will also be present on any new blogs (they will appear as default categories when MP gets activated on new blogs).

~ Step 3. Remove the +Add New Product Category link ~
The product pages in the admin allow users to add new product categories. This is a default of WordPress. You need to hide that link with specific css. This is done by styling your multisite admin area. To do this, you need to create two new files inside you mu-plugins directory.

Create a new mu-plugin called style-wp-admin.php and enter the following code. This new plugin will allow you to style you wp-admin area network-wide as you like:

<?php
/*
Plugin Name: My Admin Theme
Plugin URI:
Description: Load wp-admin.css from this same folder in order to style the admin area.
Author:
Version: 0.1
Author URI:
*/

function style_wp_admin() {
        echo '<link rel="stylesheet" type="text/css" href="' .plugins_url('wp-admin.css', __FILE__). '">';
}

// Call the function above
add_action('admin_head', 'style_wp_admin');
?>

Next, also in mu-plugins, create wp-admin.css. This is the file you can now use to style your admin area. You can use this as you like from now own, but for this particular tutorial, you need to add this code to hide the link:

#product_category-add-toggle {
    display: none;
    visibility: hidden;
}

~ Step 4. Hide the Product Categories Menu Item ~
We use Admin Menu Editor plugin to keep the dashboard menus tidy for our users accross the network. This was a natural tool to use for this task. We simply changed the permissions for the Product Categories to Super Admin and moved it to a private menu we have for Super Admins only.

All Done! You have now gained complete control over product categories and they will be deployed globally across your network, ***but only on new sites.****

PART 2: CLEANING UP THE MESS
If you had a situation like us, you might have had sellers creating their own product categories which may leave you with a lot of different and interesting categories. You are going to have to go through to each site and clean things up. This part will tell you how.

~ Step 1a. Delete all existing categories ~
Go to all the store sites with existing product categories and delete all the categories your sellers have created. I loaded them all up in different tabs in the browser and went through each one and cleared them out. Keep these tabs open, you'll need them again later.

~ Step 1b. Delete the categories from the marketplace table in the database ~
When someone creates a category it gets saved to wp_mp_terms in your database. You need to go to that table and delete any entry that is a product_category type. I also emptied the wp_mp_term_relationships table while I was at it. The data in these tables is a duplicate of data saved on the blogs, you aren't killing anything that won't be created again.

~ Step 2. Push your decided categories out to the network ~
Here, you will add your product categories to every site on your network. This is on the chance that any blog in time might become a store. You need the Network Terminator plugin for this. It works flawlessly and instantly and supports custom post types. You have to put it in yourself. Create a new folder in your plugin folder called network-terminator and put in it the file found here: https://github.com/maugly/Network-Terminator/blob/master/mau_network_terminator.php

Network Activate and you are ready to go. Simply select all the blogs in the network, add in your chosen product categories and send them out. It doesn't matter if those blogs have MarketPress active on them or not, the categories will still be added and will be ready in case they are needed in the future. I was really impressed at how well this worked.

~ Step 4. Clean up clean up taxonomy conflicts ~
Categories and tags are simply taxonomies. They are stored in the same table and differentiated by type. However, being taxonomies, they cannot have the same slugs. If one of the existing blogs already has a tag with slug the same as one of your categories, the your new category won't get added. It doesn't get the -2, it just won't be there. This is the most time consuming part.

You need to go to each blog and confirm that your product category was successfully added. I did this by checking each one under quick edit. If I found a blog that was missing one of my categories, I then had to screen through (using quick edit) all the tags on that blog to find the offender and delete it. Once the tag that is blocking your category out has been deleted, you should be able to add the category.

Given they way people are liberal with tags, this may take a while. It is labor intensive, but once you get it under control, if you properly implemented the setup in Part 1, you will never have to do this again.

~Step 5. Assign products to categories ~
The last thing you have to do, is reassign all your seller's products to your new categories. This step is more essential than it appears on the surface. Up above, in Step1a I had you delete all the categories, this made all the product uncategorized, and then you deleted all the product categories from the database. In order to repopulate the database table (it is this table which powers your global product categories and tags widget) you need to reassign and resave everything. Saving them adds them back into that table.

Go through each blog, assign each product to your appropriate categories and save. This can be done using quick-edit. It was during this process that I discovered most of my taxonomy conflicts. If you didn't find them manually, then it will become obvious here because when you sort by category in your marketplace, the product just won't be there. Tracking down and removing those taxonomy conflicts is essential.

DONE!

It is some work to get this done, but the result is well worth it. When your sellers activate Marketpress and start adding products it makes your network look very professional and prepared.

Fondeli contributed a lot information to the method in making this happen and deserves a lot of credit. to my knowledge she was one of the first ones to get global product categories working. Additionally, she uses custom template files to sort and order the global categories in the marketplace. I haven't done this yet, but hopefully, she'll come by this thread and add her two cents.