a
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudanti.
a
Follow Me
Search
Maintain the same WooCommerce categories on all shops in the network - WooCommerce MultiSite Global Cart
14644
documentation-template-default,single,single-documentation,postid-14644,cookies-not-set,eltd-core-1.1,woocommerce-no-js,awake child-child-ver-1.0.0,awake-ver-1.0,eltd-smooth-scroll,eltd-smooth-page-transitions,eltd-mimic-ajax,eltd-grid-1200,eltd-blog-installed,eltd-default-style,eltd-fade-push-text-top,eltd-header-standard,eltd-sticky-header-on-scroll-down-up,eltd-default-mobile-header,eltd-sticky-up-mobile-header,eltd-menu-item-first-level-bg-color,eltd-dropdown-slide-from-top,eltd-,eltd-fullscreen-search eltd-search-fade,eltd-side-menu-slide-from-right,wpb-js-composer js-comp-ver-5.0.1,vc_responsive
 

Maintain the same WooCommerce categories on all shops in the network

WooCommerce MultiSite Global Cart / Maintain the same WooCommerce categories on all shops in the network
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on TumblrPin on PinterestEmail this to someonePrint this page

Maintain the same WooCommerce categories on all shops in the network

Under WordPress MultiSite environment, WooCommerce can run on any sites. In a sense, that means the WooCommerce is apparently MultiSite capable and compatible. In practice, there’s no real MultiSite capability as no data is interconnected from a shop to another, every instance is self running, with no possibility to interact with the other sites in the network.

WordPress WooGlobalCart brings in a new level of data operability, mainly consisting on a single cart which can be used across all shops. Other than existing functions and features available through interfaces and filters, the code provides infrastructure for additional classes constructors, improvements which can be achieved relative easily through customized code.

Maintaining categories across all shops (or specifically any of them) is an important function in specific set-ups. As default, categories can be created for each of the shops. There is no relationship between categories and their assigned id’s to any of the other places in the network. In some cases, category synchronization across all shops is a must have option, to make everything easy to manage.

Category mirroring across all shops, can be used along many existing plugins which can run on MultiSite but does not take advantage of any relationship data. For instance affiliate plugins, dynamic pricing codes, anything which may use categories to apply specific filters can use this functionality, along with WooGlobalCart.

The following code should be placed in a file e.g. woo-gc-global-categories.php inside you /wp-contnet/mu-plugins folder. If the folder does not exist on your WordPress, you should create it manually.

The category management will be further processed within a single shop in the network, all other will inherit any update. For that reason, the WooGC_GlobalCategories__terms_table and WooGC_GlobalCategories__term_taxonomy_table constants need to be updated with the shop where the category changes will operate, accordingly. In this example, the site ID 4 is being set as “master” for others. The site ID can be found within MultiSite Dashboard admin at All Sites. For site ID 1 (default site) there will be no number in the assigned values e.g. ‘terms’ and ‘term_taonomy’.

<?php
    
    global $wpdb;
    
    define('WooGC_GlobalCategories__terms_table', $wpdb->base_prefix . '4_terms');
    define('WooGC_GlobalCategories__term_taxonomy_table', $wpdb->base_prefix . '4_term_taxonomy');
     
    function woo_gc_global_terms_get_tables()
        {
            
            $new_terms_table            =   WooGC_GlobalCategories__terms_table;
            $new_term_taxonomy_table    =   WooGC_GlobalCategories__term_taxonomy_table;    
            
            return array($new_terms_table, $new_term_taxonomy_table);
        }
        
    add_filter('terms_clauses', 'woo_gc_terms_clauses', 999, 3);
    function woo_gc_terms_clauses($clauses, $taxonomies, $args)
        {
            //check for required txonomy
            if ( empty($taxonomies) )
                return $clauses;
                
            if ( is_string($taxonomies) &&  $taxonomies !=  "product_cat")
                return $clauses; 
                
            if ( is_array($taxonomies))
                {
                    if ( count ($taxonomies)    >   1   ||  count ($taxonomies)    <   1)
                        {
                            return $clauses;
                        }
                        else
                        {
                            reset( $taxonomies );
                            if ( current($taxonomies)   !=  "product_cat")
                                return $clauses;
                        }    
                }
                
            global $wpdb, $blog_id, $WOO_GC_WORKAROUND_GLOBAL_TERMS;
            
            list ( $new_terms_table, $new_term_taxonomy_table ) =   woo_gc_global_terms_get_tables();
            
            $replace_terms_table                =   $wpdb->terms;
            $replace_term_taxonomy_table        =   $wpdb->term_taxonomy;
            
            foreach ( $clauses  as  $key    =>  $clause)
                {
                    $clauses[$key]  =   str_replace($replace_terms_table, $new_terms_table, $clause);
                    $clauses[$key]  =   str_replace($replace_term_taxonomy_table, $new_term_taxonomy_table, $clause);
                }
            
            $WOO_GC_WORKAROUND_GLOBAL_TERMS['run_seccond_pass'] =   TRUE;
            
            return $clauses;
        }
        
    /**
    * Lack of available filter for get_terms, use this workaround for a seccond replacement for terms table
    */
    add_filter('query', 'woo_gc_query');
    function woo_gc_query( $query )
        {
            global $WOO_GC_WORKAROUND_GLOBAL_TERMS, $wpdb;
        
            if ( isset($WOO_GC_WORKAROUND_GLOBAL_TERMS['run_seccond_pass'])    &&  $WOO_GC_WORKAROUND_GLOBAL_TERMS['run_seccond_pass']    === TRUE )
                {
                    global $wpdb;
                    
                    unset($WOO_GC_WORKAROUND_GLOBAL_TERMS['run_seccond_pass']);   
                    
                    list ( $new_terms_table, $new_term_taxonomy_table ) =   woo_gc_global_terms_get_tables();
                    
                    $replace_terms_table                =   $wpdb->terms;
                    $replace_term_taxonomy_table        =   $wpdb->term_taxonomy;
                    
                    $query  =   str_replace( $replace_terms_table, $new_terms_table, $query);
                    $query  =   str_replace( $replace_term_taxonomy_table, $new_term_taxonomy_table, $query);
                    
                }
                  
            return $query;
        }  


?>