skip to Main Content

we have a woocommerce shop for bike parts.
Our parent category name is "Motorcycle-Parts". Subcategories are the motorcycle brands, like:

Motorcycle-Parts
      => Suzuki
      => Honda
      => Yamaha
      ...

Inside the subcategories, we have the products. These products have an attribute called "ccm" (its the cubic capacity,with numeric values).

Now we try to list all existing "ccm" values from the products of each category underneath the category name, like:

Motorcycle-Parts
      => Suzuki 
          - 300
          - 380  
          - 550   
          - ...   
      => Honda
          - 600
          - 650  
          - 700   
          - ...  
      => Yamaha
          - 420
          - 650  
          - 860   
          - ...  

So, if for example a product is inside the category "Suzuki" with the attribute pa_ccm (e.g. 500), we want to automatically list that value (500) underneath the category name, so people see, that inside the category Suzuki one (or more) Products exist that have the pa_ccm value 500.

I tried a few things with _get_term_hierarchy, but my skills are limited here. So no results.
Thanks for help and ideas.

2

Answers


  1. Chosen as BEST ANSWER

    So this is what I have now - Working!

    //  Get all categories
    $args = array(
         'taxonomy'     => 'product_cat',
         'orderby'      => 'name',
         'parent' => 8543
    );
    $all_categories = get_categories($args); //all categories are stored here.
    
    foreach ($all_categories as $cat) {
    
      echo "<br><br>Category: " .$cat->name ."<br>";
    
      $product_args = array(
          'category'  => $cat->slug,
          'limit' => -1
      );
    
      //get products for each category and hide duplicate term->names
      $unique_term_names = array();
      foreach( wc_get_products($product_args) as $product ) {
    
      // The attribute slug
      $attribute = 'pa_hubraum';
    
      // Get attribute term names in a coma separated string
      $term_names = $product->get_attribute( $attribute );
    
      if( ! in_array( $term_names, $unique_term_names ) ) :
         // add to array so it doesn't repeat
         $unique_term_names[] = $term_names;
        
      echo '<p>' . $term_names . '</p>';
      endif;
      }
    }
    

    This code does list the $term_names from attribute "ccm" underneath the categories and hides duplicate values/term->names.

    Last little problem: As we have many products in these categories with alot of attributes and terms, I want to ask if there is a more performand way to optimise that function/query above? Does someone have an idea if its possible to programm that in a way to make the loading time faster?


  2. You may have to loop each category, each product, each attribute and then each term in order to get the desired structure. For the purpose of clarity, I am leaving parent-child category part and presenting rest of the code as below:

    //  Get all categories
    
      $args = array(
             'taxonomy'     => 'product_cat',
             'orderby'      => 'name'
      );
     $all_categories = get_categories($args); //all categories are stored here.
     
     foreach ($all_categories as $cat) {
     
     echo "Category: " .$cat->name ."<br>";
     
         $product_args = array(
            'category'  => $cat->slug;
        );
        
        //get products for each category
        
        foreach( wc_get_products($product_args) as $product ) {
        
        //get attributes for each product
        
            foreach( $product->get_attributes('pa_ccm') as $attr_name => $attr ){
            
            //get terms for each attribute
    
                foreach( $attr->get_terms() as $term ){
    
                echo "CCM: " .$term->name ."<br>";
            }
    
        }
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search