Advertisement
LoganYoung87

Untitled

Apr 14th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. function woocommerce_category_id() {
  2.     global $post;
  3.     $prod_terms = get_the_terms( $post->ID, 'product_cat' );
  4.  
  5.     if ($prod_terms != null) {
  6.     foreach ($prod_terms as $prod_term) {
  7.         // gets product cat id
  8.         $product_cat_name = $prod_term->term_id;
  9.        
  10.         // gets an array of all parent category levels
  11.         $product_parent_categories_all_hierachy = get_ancestors( $product_cat_name, 'product_cat' );  
  12.        
  13.         // This cuts the array and extracts the last set in the array
  14.         $last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
  15.         foreach($last_parent_cat as $last_parent_cat_value){
  16.            
  17.         // $last_parent_cat_value is the id of the most top level category, can be use whichever one like
  18.         $product_id = $last_parent_cat_value;
  19.        
  20.         $args = array('hierarchical' => 1, 'show_option_none' => '', 'hide_empty' => 0, 'parent' => $product_id, 'taxonomy' => 'product_cat' );
  21.        
  22.         $subcats = get_categories($args);
  23.      
  24.         if ( is_product_category() ) {
  25.                
  26.         foreach ($subcats as $sc) {
  27.            
  28.             if(get_queried_object()->slug == $sc->slug) { $class = ' class="current"'; }
  29.             else { $class = ''; }
  30.            
  31.             $link = get_term_link( $sc->slug, $sc->taxonomy );
  32.            
  33.             echo '<li'.$class.'><a href="'. $link .'">'.$sc->name.'</a></li>';
  34.            
  35.             }
  36.              
  37.         } elseif ( is_product() ){
  38.        
  39.  
  40.         foreach ($subcats as $sc) {
  41.            
  42.             if(get_queried_object()->slug == $sc->slug) { $class = ' class="current"'; }
  43.             else { $class = ''; }
  44.            
  45.             $link = get_term_link( $sc->slug, $sc->taxonomy );
  46.            
  47.             echo '<li'.$class.'><a href="'. $link .'">'.$sc->name.'</a></li>';
  48.            
  49.             }
  50.        
  51.         } else {
  52.              
  53.              
  54.          $args2 = array('hierarchical' => 1, 'show_option_none' => '', 'hide_empty' => 0, 'parent' => 0, 'taxonomy' => 'product_cat' );
  55.        
  56.         $subproducts = get_terms( 'product_cat', $args2);
  57.        
  58.         foreach ($subproducts as $subproduct) {
  59.         $link2 = get_term_link( $subproduct->slug, $subproduct->taxonomy );
  60.         echo '<li><a href="'.$link2.'">'.$subproduct->name.'</a></li>';
  61.         }
  62.        
  63.          }
  64.            
  65.         }
  66.     }
  67.     else {
  68.         echo '<p>No items found.</p>';
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement