Advertisement
helgatheviki

WooCommerce URL Rewrites for Multiple Taxonomies

Mar 19th, 2012
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1.  
  2. /*
  3.  *  Add rewrite Rules for WooCommerce product-category|product-tag + attribute taxonomy
  4.  *  ex: site.com/product-category/bacon/color/red ,etc
  5.  */
  6.  
  7. function mv_add_rewrite_rule(){
  8.     global $woocommerce;
  9.    
  10.     /**
  11.      * Slugs
  12.      **/
  13.     $shop_page_id = woocommerce_get_page_id('shop');
  14.        
  15.     $base_slug = ($shop_page_id > 0 && get_page( $shop_page_id )) ? get_page_uri( $shop_page_id ) : 'shop';
  16.            
  17.     $category_slug = (get_option('woocommerce_product_category_slug')) ? get_option('woocommerce_product_category_slug') : _x('product-category', 'slug', 'woocommerce');
  18.        
  19.     $tag_slug = (get_option('woocommerce_product_tag_slug')) ? get_option('woocommerce_product_tag_slug') : _x('product-tag', 'slug', 'woocommerce');
  20.        
  21.     $attribute_taxonomies = $woocommerce->attribute_taxonomies;  
  22.     if ( $attribute_taxonomies ) :
  23.         foreach ($attribute_taxonomies as $tax) :
  24.  
  25.             $attribute = strtolower(sanitize_title($tax->attribute_name));
  26.                 $taxonomy = $woocommerce->attribute_taxonomy_name($attribute);
  27.  
  28.             if(get_option('woocommerce_prepend_shop_page_to_urls')=="yes"){
  29.                 add_rewrite_rule( $base_slug .'/'. $category_slug.'/(.+?)/'.$attribute.'/([^/]+)(/page/([0-9]+)?)?/?$',
  30.                     'index.php?product_cat=$matches[1]&'.$taxonomy.'=$matches[2]&paged=$matches[4]',
  31.                     'top' );
  32.                 add_rewrite_rule( $base_slug .'/'. $tag_slug.'/(.+?)/'.$attribute.'/([^/]+)(/page/([0-9]+)?)?/?$',
  33.                     'index.php?product_tag=$matches[1]&'.$taxonomy.'=$matches[2]&paged=$matches[4]',
  34.                     'top' );
  35.             } else {
  36.                 add_rewrite_rule( $category_slug.'/(.+?)/'.$attribute.'/([^/]+)(/page/([0-9]+)?)?/?$',
  37.                         'index.php?product_cat=$matches[1]&'.$taxonomy.'=$matches[2]&paged=$matches[4]',
  38.                         'top' );
  39.                 add_rewrite_rule( $tag_slug.'/(.+?)/'.$attribute.'/([^/]+)(/page/([0-9]+)?)?/?$',
  40.                         'index.php?product_tag=$matches[1]&'.$taxonomy.'=$matches[2]&paged=$matches[4]',
  41.                         'top' );
  42.             }
  43.  
  44.     endforeach;
  45.     endif;
  46.  
  47.  
  48.  
  49. }
  50. add_action('init','mv_add_rewrite_rule');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement