aristath
Supreme Deity
WPMU DEV Staff
Lifetime member
Support Hamster Likes (0)
I'm building a project that has the following:
1. A "template" blog. Every blog users create is a clone of this one.
2. This "template" blog has MANY (800+) product categories which are of course cloned to all other sites.
3. The product categories terms are organized hierarchically.
4. Any updates to the template-blog's product categories are synced to all other sites on my network.
So far so good...
When users create a new product on their site, they choose a category (using this plugin has made it really easy to choose and navigate all those terms).
Here comes the "but" part....
On the "global" store I have to display all product categories so that users can navigate and choose what they want to see BUT global product hierarchies are non-hierarchical. As you can imagine, having a list of that many categories sorted alphabetically with no structure at all is not functional and makes no sense.
So my question-request is this: How to display network-wide product categories hierarchically??? I'm sure I'm not the only one who would benefit from something like this!
Responses (57)
People Person (joined February 2012) Likes (0)
aristath,
You are not the first to ask for this! :)
I know it's been a common request as of late. Unfortunately the developer for this plugin is going to be away from his desk for a bit, so pinging him for a workaround will not prove successful in the short-term :)
I recall someone saying they used a plugin that helped with this...
Perhaps http://wordpress.org/extend/plugins/hierarchical-pages/
Not 100% sure, however if this should work please let me know, if not, let me know all the same.
I think we should definitely work to find a great alternative to this until we can get the developer to make an update or feedback on this one :)
Best,
Kimberly
Support Hamster (joined March 2012) Likes (0)
thank you for your reply.
I will try to find a workaround or a way to "hack" marketpress core to achieve this.
If I manage to do something I will post back here..
Support Hamster (joined March 2012) Likes (0)
I was trying to find a way to do this and I just thought of sharing those thoughts with others. Maybe someone will come along and help out.
The http://wordpress.org/extend/plugins/hierarchical-pages/ outputs a hierarchical list of our site's categories. Not our global categories... The way the database is structured on Marketpress makes this task a bit difficult. So here's a really "dirty" suggestion:
Let's say that the global categories terms urls have a structure like marketplace/categories/slug.
The "normal" categories url's are something like store/products/category/slug.
Instead of trying to display the global categories hierarchically, why not try to display the normal product categories (which are by default hierarchical) and rewrite their urls to the global categories slugs?
On the marketpress-ms.php file, if we examine the mp_global_categories_list function, we see that the URLs of the global product categories come from this line:
$link = get_home_url( mp_main_site_id(), $settings['slugs']['marketplace'] . '/' . $settings['slugs']['categories'] . '/' . $tag['slug'] . '/' );People Person (joined February 2012) Likes (0)
I've officially notified the developer and marked this as a Feature Request :)
Support Hamster (joined March 2012) Likes (0)
Thank you!
I think this is REALLY important...
I will try to analyse this problem as much as I can, hoping something will be of assistance to anyone that tries to solve this.
When a product is created on a site of the network, This is what happens on the database:
An entry is created on the wp_[blogID]_posts table. The info stored here contains all the info of the product except the product category.
An entry is created on the wp_[blogID]_term_relationships. The info stored here is the id of the product post and the id of the product category. This way wordpress knows the category of a product.
Wordpress retrieves the names of the categories from the table wp_[blogID]_terms and determines their hierarchy from the wp_[blogID]_term_taxonomy table.
At the same time, when a product is created on a site, these things also happen:
An entry is created on the wp_mp_products. This table is used from the main site to display global products.
An entry is created for the category of the product (if it doesn't already exist) in the wp_mp_terms table.
An entry is created on the wp_mp_term_relationships table that tells wordpress which products belongs to a category.
Now for the solution:
There is no apparent way for the product categories to have a hierarchy, unless these conditions are met: Product categories are synced throughout the network of sites and they are present on the main site.
In my opinion there should be an option in marketpress core. A simple checkbox
that allows network admins to choose syncing all categories from the main site to all other network sites. In order to sync all product categories, a solution based on this is what most people use so this would be a good place to start.
Once the network admin chooses this, THEN it will be possible for hierarchies on global categories to work correctly.
HOW?
Though there is no column called "parent" on any of the "wp_mp_....." tables, there is such a column on the wp_term_taxonomy table.
Unfortunately the terms on the wp_terms and wp_mp_terms have different ids. BUT their "slug" column is identical.
So here's what should be done:
1. Wordpress looks for the category on the wp_mp_terms table
2. For each category present there, wordpress looks for the id in the wp_terms table
3. retrieve the parents of that term id from the wp_term_taxonomy table
4. Determine the hierarchy of the terms from the above step
5. Return the results as a hierarchical list
Support Hamster (joined March 2012) Likes (0)
I forgot to mention that this sync should run on cron so that network admins don't have to do it manually every time a term is added.
Developer (joined January 2012) Likes (0)
@aristath, One other hole in the approach you suggested is that term slugs are unique. So if one site has a child term C under parent term A and a second site has the same child term C under a different parent B things will not merge. Try this.
In your MarketPress Products Categories create a top category 'Widgets'. Make sure it has the term slug 'widgets'. Create a child category 'Sub-Widgets'. Assign the parent as 'Widgets'. Make sure it has the term slug 'sub-widgets'. Should look like the following
Widgets (widgets)
-> Sub-Widgets (sub-widgets)
Now create a new top category 'Other-Widgets'. Make sure it has a term slug 'other-widgets'. Now create another child category 'Sub-Widgets'. Assign the parent as 'Other-Widgets'. Try to make sure the term slug is 'sub-widgets'.
Other-Widgets (other-widgets)
-> Sub-Widgets (???)
Developer (joined January 2012) Likes (0)
(sorry, didn't mean to submit)
Maybe a better solution would be have the primary site contain the Product Categories and Product Tags Taxonomies. All the sub-sites would query the primary site terms tables. This would mean the site admins would need to agree on the category hierarchy.
Basically this can be done via a file on the WP_query object. Would be seamless to the users.
Support Hamster (joined March 2012) Likes (0)
That's why I suggested that ALL sites should have their terms synced to reflect the terms of the main site.
I agree, that would also mean less load on the db and php since not all sites would have to have the categories cloned and synced all the time.
So combining things from my previous post and yours, I now revise my previous statement to this:
There should be an option in marketpress core. A simple checkbox visible to network admins. When checked, all queries would be rewriten to use the terms of the main site istead of the terms from each site individually.
This means both when entering a new product AND when quering products for lists etc.
How is that?
Developer (joined January 2012) Likes (0)
Agreed. I'm actually working on something similar for another soon to be released plugin. So hopefully I can share that logic with MarketPress.
Support Hamster (joined March 2012) Likes (0)
Thank you!
If you need any beta testers I'll be the first to try this out... I've been working on a big project for 6 months now and it's actually the only reason I haven't gone live yet!
Support Hamster (joined March 2012) Likes (0)
Just an update...
Wordpress 3.4 (released a few hours ago) Introduces term_is_ancestor_of()
Could be useful here.. :)
Support Hamster (joined March 2012) Likes (0)
@PaulM I found this, please advise...
From what I understand, what this does is tell all sites to use for their terms NOT their individual tables on the db (wp_2_terms, wp_3_terms etc) but instead use the main blog's tables (wp_terms, term_taxonomy and term_relationships).
I haven't tested this, but I suppose it could do the trick. :)
The thing that troubles me the most is this line:
$wpdb->term_relationships = "wp_term_relationships";If we use this, then things might break 'cause post#1 on site#1 has different terms than post#1 on site#2.
Maybe we should use the first 2 tables from the main site and the third one individually on each blog?
The logic seems to be ok... I just want another opinion on this.
Please advise...
If this works, implementing the checkbox we were talking about a few days ago on the Marketpress Superadmin's page and writing an if{..........} shouldn't be more than 10 minutes work. :P
Support Hamster (joined March 2012) Likes (1)Liked by: 
Well, I made a plugin with the above code and it does seem to work.
All products from all sites use the wp_terms and wp_term_taxonomy tables of the db.
So now, all sites use the terms from the main blog.
The downside to this is that it doesn't only apply to product categories, but ALL terms. including post categories and tags.
For some use cases, including my own that's not actually a bad thing!
Now for the other issue:
MarketPress uses for the main blog these tables: mp_terms and mp_term_relationships.
I need to make it work with wp_terms and wp_term_relationships instead.
This will solve most issues on this forum concerning:
global product categories,
main blog categories hierarchy,
less clutter in the database which will result in improved performance
so PLEASE if anyone else is watching this thread, say your thoughts on this...
Personal advice. Not obligatory but highly recommended:
Convert those tables to InnoDB instead of MyISAM to avoid table locking.
Better yet, convert the whole db to InnoDB (if you have MariaDB or Percona, that's even better of course).
If you're on a VPS, edit your my.cnf file and set the default db engine to InnoDB so that all future blogs tables on the db are InnoDB and not MyISAM.
Support Hamster (joined March 2012) Likes (0)
ok, so marketpress-ms.php is full of mp_terms.
What if I just search and replace all instances of mp_terms with plain terms ????????
I wonder what would happen then....
Member (joined May 2012) Likes (0)
@aristath
Thank for bringing this up for the community. This topic of syncing of terms seems very important and should not be overlooked in future releases of MarketPress.
I don't have any solution, but I am posting to show my support!
We are early on in our development using MP and a network wide store, but if we come up with a solve in the near future I hope to post it back here for you to see.
thanks again.
Support Hamster (joined March 2012) Likes (0)
@yoshimotor thank you for showing your support!
In order to sync terms, the BEST solution I have found so far is this:
Inside your "wp-content, create a folder called "central-taxonomies".
inside that folder, create a file called "central-taxonomies.php".
Put this inside your file:
Now all sites use the main blog's categories instead of their own.
I tested it both for existing sites as well as newly-created ones and it works flawlessly.
Support Hamster (joined March 2012) Likes (0)
I forgot to mention... If you want all of your sites to use the main blog's terms, then simply network-activate the plugin.
If you want just some blogs to use the main site's terms instead of their own, then activate the plugin for those sites.
That's all.. :)
Support Hamster (joined March 2012) Likes (0)
In case you want a ready plugin, I just found someone has done the exact same thing!
http://wordpress.org/extend/plugins/mu-central-taxonomies/
Member (joined May 2012) Likes (0)
Amazing, thank you..!
Support Hamster (joined March 2012) Likes (0)
I'm glad I could help!
Thank you for your points too.. I appreciate it!
Support Hamster (joined March 2012) Likes (0)
I've been looking at the marketpress-ms.php file...
There A LOT of stuff going there so it 's going to take me a few days to figure out the best approach.
The thing is that I don't want to do something to my installation that's going to be incompatible with any future implementation the WPMUdev team comes up with.
Any changes I might do to the marketpress-ms.php file will alter the way MarketPress queries the database so I'm a little reluctant.
@PaulM is there anything specific you could tell me about the way you 're planning to implement this? Even something completely vague can trigger a useful discussion resulting in something more specific and complete.
I hope to hear from you soon.
Keep up the great work!
Member (joined April 2009) Likes (0)
@aristath, great stuff!
Paul is working with our lead developer (Aaron) on things here. Aaron's out for the next couple weeks so major core changes shouldn't be expected during this time.
However, both developers are subscribed to this thread and as we've had several members interested in a solution like this we'll all be paying close attention :)
Thanks again.
Support Hamster (joined March 2012) Likes (0)
@masonjames thank you for your "you are not alone" comment, it's encouraging and reassuring. It's understandable that you guys have more stuff to deal with (after all, you have more than 140 plugins) so I'll just keep on trying to find a solution or something to help out.
Keep up the good work!
Developer (joined January 2012) Likes (0)
@aristath, Reading through this long thread. You have been busy! ;)
So the code you posted using the function 'central_taxonomies' on the init hook. Would that not force all taxonomies to use the central terms tables? Meaning this is not a solution just for MarketPress. If the site is using the regular blog categories, tags these would also be rounded to the primary site terms database. Not that this is a bad things. Just wanted to bring this up.
On the other parts of your posts. I can say that I have a better solution at this time. Still digging into MarketPress trying to come up with a better recommendation without breaking things.
Developer (joined January 2012) Likes (0)
@aristath, So on your post related to using the global wp_terms, term_taxonomy and wp_term_relationships instead of the MarketPress wp_mp_terms and wp_mp_term_relationships would be to setup a filter on the WP $query.
But first a warning about doing this. I'm not sure yet on the implications.
Developer (joined January 2012) Likes (0)
Maybe for readability you would prefer a gist https://gist.github.com/2954788
Support Hamster (joined March 2012) Likes (0)
@PaulM
yes, that's exactly what happens (that's the downside to this implementation). The benefits are well... a much lighter database if you have lots of categories (like I do) and let's face it, most people when building a marketplace don't worry about their blog's categories that much. In some use cases they might... but this is an e-commerce solution capable of multi-site installations.
This is for the marketpress-ms.php file, right?
I'll test it later tonight...
Developer (joined January 2012) Likes (0)
Agreed, most 'shopping' sites would use less of the normal blog tools. So having things central might be best. Still if you even need to split a site to its own install this would make extracting the data a little more difficult. But I say worry about that if ever the time comes.
As for the code I provided this would go into your theme's functions.php or somewhere. I try not to recommend hacking the plugin directly. As it would prevent future upgrades. Let me know. Again, I'm still trying to figure out if this effects anything else. So far not seeing an issue.
Support Hamster (joined March 2012) Likes (0)
ok, I'll try it in about an hour or so. (I'll try this on a clean installation...) :)
Support Hamster (joined March 2012) Likes (0)
I put this function on my theme's functions.php file and tried creating a product. I tried it both with the 'central_taxonomies' previously posted function and without it.
When creating a product there was no registration in the database on the wp_term_relationships and wp_mp_term_relationships tables
Am I missing something?
The log is here: https://gist.github.com/2955500
Note: I tried this with pre-existing product categories.
Developer (joined January 2012) Likes (0)
I just saw a part of my provided code which was wrong. Note to self have coffee before writing code. The issue was on the second str_replace. Should be fixed.
I've updated the gist https://gist.github.com/2954788
Support Hamster (joined March 2012) Likes (0)
I'm afraid I got a bit confused...
What exactly is it that we're trying to achieve here with this?
Rewrite the queries to use the wp_terms and wp_term_relationships, right?
The code seems ok to me, I just can't get it to work. All updates are applied to the wp_mp_terms and wp_mp_term_relationships tables in my db.
I tried it with 2 themes as well as in the marketpress-ms.php file (just for the sake of testing) and still nothing. I must be doing something wrong, maybe it's the fact that I've been working 20 hours non-stop. I need coffee too...
Developer (joined January 2012) Likes (0)
I say for now take a break. Catch a nap. I'll double check the logic once I finish with another fire which came up. Oh and you should disable the plugin of filter for the central_taxonomies from previous.
Support Hamster (joined March 2012) Likes (0)
I still can't get this to work...
Anyway, I'm going to rest now but I hope to hear from you tomorrow.
Member (joined June 2012) Likes (0)
This is exactly the kind of thing I need right away for a client, and is what I thought MarketPress would already have built in.
Great progress so far and I'll be watching closely for developments :)
Support Hamster (joined March 2012) Likes (1)Liked by: 
Yes, I admit you would expect this to be available by default. Unfortunately it's not there yet but we're getting closer...
Support Hamster (joined March 2012) Likes (1)Liked by: 
@PaulM the purpose of the code you shared 2 days ago here https://gist.github.com/2954788 is to rewrite all queries to use the terms and term_relationships tables instead of the mp_terms and mp_term_relationships, right?
Since those tables are only called in the main site when saving/updating a product and when trying to retrieve the product categories, why shouldn't I use this in combination with the previous function that forces all sites to use the main site's taxonomy tables?
I don't see where the conflict would be...
And if this works, we 'll still have to rewrite the global categories widget to display the categories hierarchy, am I right?
Before taking any more steps I just want to be sure of the logic behind this so we'll move a bit faster (I'm on a deadline... I must find a way to implement this in the next 7-10 days maximum, without losing compatibility with future versions of MarketPress.)
Developer (joined January 2012) Likes (0)
@aristath, That code I shared was a way to address one of your previous posts in this thread about the plugin using the mp_ tables instead of the global wp_terms, etc tables. You were asking for something to get you started and I thought that is what I was doing. It was not offered as a total solution but for something to possibly get you started to your solution. Sorry I have not been back on this thread in the last 2 days. My focus has been pulled unfortunately in many different directions.
Good to know about your deadline. But I have concerns on f this is actually possible at all with MarketPress. I'll try and find a few hours later this morning to focus on this and offer more assistance.
Support Hamster (joined March 2012) Likes (0)
@PaulM every bit of assistance and information you have to offer is crucial to me!
Thank you again for taking the time to look into this...
Developer (joined January 2012) Likes (0)
@aristath thanks for you patience. I'm working on a issue with the MailChimp-Sync plugins I started on last night (my time) then I'll be able to get back on this thread and catch up on where you are, or are not.
Support Hamster (joined March 2012) Likes (0)
I'm under the impression that it's going to be simpler to copy and alter the mp_global_categories_list function to my functions.php file...
Adding some things to the query so that the list of categories is pulled from the wp_mp_terms table (as it is by default) BUT at the same time the hierarchy of the terms is pulled from the wp_terms table.
Possible? that's what I'm trying to figure out right now.
Unfortunately I'm not too good when it comes to queries so for now I'm studying how to achieve it.
Developer (joined January 2012) Likes (0)
@aristath, I'm setting up a clean Multisite install with MarketPress network activated. I think me current general use development site is tainted with too many hacks.
So I have a new install with 5 sites (site1-site5). As I mentioned MarketPress is network activated. On site1 and site2 I've setup 2 products each. On each site/product combination I have setup a top-level category 'SiteX' where 'X' is the site number. Then on each I've setup a sub-category 'Products' nested beneath the 'SiteX' parent.
When I view the MySQL tables wp_mp_products, wp_mp_terms and wp_mp_term_relationship I see the various entries.
So now what to do? I agree with you this might be easier attacking from a new direction. I'm actually pretty good at SQL. So I'm starting in that direction. I'll let you know via this thread if I make any progress.
Support Hamster (joined March 2012) Likes (0)
Just an update on this, I got caught up in the theme designing of the site and this issue got left behind a bit. It's not forgotten and it's a must-have. I 'll deal with it again tomorrow...
In the meantime if anybody has any ideas leave your comment.
Support Hamster (joined March 2012) Likes (0)
I'm still here and this issue needs resolving... If any devs out there can help out it would be really appreciated!
People Person (joined February 2012) Likes (0)
aristath, just a note to say that Paul has been tied up assisting in the absence of one of our developers, he's not forgotten about you I'm sure, just very busy :)
He has done a stellar job with helping you here on a Feature Request, high fives to him!
Support Hamster (joined March 2012) Likes (0)
I'm sure he like others are just too busy.
And I'm not being ungrateful, I have expressed my gratitude towards Paul and others and I can't stress enough how helpful everyone has been.
But after 2 weeks of inactivity this issue had almost disappeared from the issue queue, I'm just posting here as a reminder and so that people can see this. The more people see this, the more likely it is that someone will post something back! Who knows, I may even get a response like "hey, you know what, I had that problem too and I solved it doing this and that!" (miracles do happen.. :P )
People Person (joined February 2012) Likes (0)
Agreed!
Fingers crossed for you!
Hopefully this activity will bump it a bit :)
Lead Developer (joined May 2009) Likes (0)
Hi there, taxonomies are stored per site in multisite, they are not in any way synced across sites. I have seen many people desire this, but no one to my knowledge has successfully implemented this, mostly because of a lack of needed hooks in core from my recollection. There may be a way around this
Of course that would be a prerequisite to making the MarketPress or Post Indexer plugin's indexing tables display things hierarchically.
I don't really have any advice for you sorry! It would be a rather complex undertaking.
Support Hamster (joined March 2012) Likes (0)
I'm already syncing product categories network-wide.
These days I'm actually working on the logic of the query that's involved in achieving a hierarchical global categories list and after that's finished I'll sum everything up and make it a plugin for everyone that's interested...
It will involve the following:
1. Auto-syncing product categories across all sites of a network (will run either manually or on cron... still trying to decide which is best.)
2. A function creating the hierarchical list
Step 1 is done and works perfectly fine for me...
Now I'm on to step 2.
I've illustrated the logic behind it here (using the tables I'm planning to use in my query): https://docs.google.com/drawings/d/1hJ4pAKzBR1jzTsrPqDQIsukxogZLIAbUF1jCSBMpq9Q/edit
Of course it won't be done using wordpress queries but using raw sql. Sometimes for complex things it's easier this way...
Member (joined August 2012) Likes (0)
Hi @aristath !
Amazing work ! I'm searching a solution for hierarchy on global categories too.
Do you have something since July ?
I though about a solution, but I'm not sure that it's possible :
The widget marketpress product categories permet to show hierarchy.
If we have all the global products categories on the main site, we can show hierarchy with the product categories widget (and not the global product categories widget) on the main site.
So, maybe is it possible to keep this widget and put it on all the sub store, right ?
Support Hamster (joined March 2012) Likes (0)
The problem is that the global categories are in a different table than that of the product categories in the db.
So it's not possible to use the product categories widget to display global categories.
And though the have the same name, they have different IDs!
Unfortunately I lack the knowledge to accomplish the SQL query needed, though I have illustrated it above.
If anyone can write the query I'd be more than happy to write the rest of the stuff needed!
Member (joined August 2012) Likes (0)
But if global categories are also on sub-store :
http://premium.wpmudev.org/forums/topic/global-product-categories-solved-a-tutorial
With this tutorial, all the categories are on the default template blog, so if i list them with the product categories widget on the default template sub-store, I have all the categories ! Isn't it ?
Support Hamster (joined March 2012) Likes (0)
Well, it depends on what you mean by "global categories".
If by "global categories" you mean categories that are present in all the sites of your network, then there's no issue. It's easily doable!
These categories link to /store/products/category/<category>
But if by "global categories" you mean the categories that the main site creates containing products from all sub-stores, then what I said earlier applies. These "pseudo"-categories are not created by you. Everytime a category is added on one of the sub-sites, a "pseudo"-category is created on the main site that references the products of the sub-store. That's where the problem lies and these are the categories that so far don't have a hierarchy.
These categories link to /marketplace/categories/<category>
Member (joined March 2012) Likes (0)
@aristath
If you get a chance shoot me an email, i have two things that may help you! i will let you decide how to post them. mark (at) mtb1701 (dot) com
If you recall i was looking for this a few months back! I do not know how i missed this thread. :/
Member (joined May 2012) Likes (0)
I would too love to see this info ... if possible! :)
Member (joined March 2012) Likes (0)
@yoshimotor
here is something another member has suggested, but i have not tried it, but he claims it works, so see what you think.
http://premium.wpmudev.org/forums/topic/marketpress-multi-site-master-products-possible#post-297018
The other option is custom tailored for @aristath "Multi Tool Box" lol; i found something that might be a perfect fit for him, because we use some of the same Non-WP tools, but it is useless to most people here!
Thanks!
Be Blessed!
WordPress Questions?
We've got answers!
Find out more »