Search
Maintain the same WooCommerce categories on all shops in the network - WP Global Cart
14644
documentation-template-default,single,single-documentation,postid-14644,theme-awake,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-6.3.0,vc_responsive
 

Maintain the same WooCommerce categories on all shops in the network

WP 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

Update: The recent WordPress and WooCommerce changes deprecated the current article code example, and thus will not be maintained anymore.

Under WordPress MultiSite environment, WooCommerce can run on any site. 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 related 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-content/mu-plugins folder. If the folder does not exist on your WordPress, you should create it manually.

Before starting, ensure you create a database backup for easier recovery, just in case something goes wrong. Also, the custom code may fail while using certain plugins, mainly the ones altering the default WordPress taxonomy queries.

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');
    define('WooGC_GlobalCategories__taonomy',                                   "product_cat");
     
    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 !=  WooGC_GlobalCategories__taonomy)
                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;
        }
    
    
    /**
    * Hold the taxonomy detail which will be used later
    */
    add_filter('wp_insert_term_data', 'woo_gc_wp_insert_term_data', 10, 3);
    function woo_gc_wp_insert_term_data( $data, $taxonomy, $args )
        {
            global $WOO_GC_WORKAROUND_GLOBAL_TERMS;
            
            if ( $taxonomy  !=  "product_cat" )
                return $data;
                
            $WOO_GC_WORKAROUND_GLOBAL_TERMS['wp_insert_term_data']['taxonomy']  =   WooGC_GlobalCategories__taonomy;
            
            return $data;
            
        }
    
        
        
    /**
    * 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;
            
            //check for term_exists() query  OR  run_seccond_pass       
            if ( 
                preg_match( "/SELECT tt.term_id, tt.term_taxonomy_id FROM wp(_\d+)?_terms AS t INNER JOIN " . $wpdb->base_prefix . "(_\d+)?_term_taxonomy as tt ON tt.term_id = t.term_id WHERE t.term_id = \d+ AND tt.taxonomy = '". WooGC_GlobalCategories__taonomy ."'/", $query )
                || preg_match( "/SELECT tt.term_id FROM wp(_\d+)?_term_taxonomy AS tt WHERE tt.taxonomy = '". WooGC_GlobalCategories__taonomy ."' AND tt.term_taxonomy_id IN \('\d+'\)/", $query )
                || ( 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;
            
        }  
        
    //ensure to return the actual terms and not the local data
    add_filter('get_object_terms', 'woo_gc_get_object_terms', 99, 4 );
    function woo_gc_get_object_terms ( $terms, $object_ids, $taxonomies, $args )
        {
            
            if ( ! is_array ( $taxonomies )   ||   ! in_array ( WooGC_GlobalCategories__taonomy, $taxonomies ) )      
                return $terms;   
            
            $_args   =  $args;
            $_args['taxonomy']  =   array ( WooGC_GlobalCategories__taonomy );
            $_terms =   get_terms ( $_args );
            
            //filter the terms
            foreach ( $terms as $key    =>  $term )
                {
                    if ( $term->taxonomy    ==  WooGC_GlobalCategories__taonomy )     
                        unset ( $terms[ $key ] );
                }
                
            $terms  =   array_values ( $terms ); 
            
            foreach ( $_terms as $term )
                $terms[]    =   $term;
            
            return $terms;   
        }


?>
2
0
Would love your thoughts, please comment.x
()
x