Support

Account

Home Forums Front-end Issues How to filter/query Woocommerce category using ACF select field

Helping

How to filter/query Woocommerce category using ACF select field

  • Context/Issue

    I’m looking for a way to loop through children categories in Woocommerce based on an ACF select field. Essentially, the Woocommerce categories will be grouped together based on what ACF field the admin has chosen. Here https://imgur.com/a/OkKQZTu is an example showing you the ACF field under the category menu.

    Essentially, I want the results to appear like this:

    Group of categories based on ACF Select Field choice A:
    Woocommerce child category 3
    Woocommerce child category 5
    Woocommerce child category 6

    Group of categories based on ACF Select Field choice B:
    Woocommerce child category 1
    Woocommerce child category 4

    Group of categories based on ACF Select Field choice C:
    Woocommerce child category 2

    Code:
    Here is my code so far. With this code, I’m able to get the first level of the child categories and their children displayed. But, again, I don’t want them to be grouped by their parent category. Instead, I want them to be grouped by the ACF select field choice.

    
    <?php 
    
      $args = array(
              'taxonomy' => 'product_cat',
              'hide_empty' => false,
              'parent'   => 2342,
          );
      $product_cat = get_terms( $args );
    
      foreach ($product_cat as $parent_product_cat)
      {
    
      echo '
          <ul>
            <li>
            <a href="'.get_term_link($parent_product_cat->term_id).'">'.$parent_product_cat->name.'</a>
            <ul>
              ';
              $child_args = array(
                  'taxonomy'    => 'product_cat',
                  'hide_empty'  => false,
                  'parent'      => $parent_product_cat->term_id,
              );
      $child_product_cats = get_terms( $child_args );
      foreach ($child_product_cats as $child_product_cat)
      {
        echo '<li>-<a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
      }
    
      echo '</ul>
          </li>
        </ul>';
      }
    
     ?>
    

    I’ve been able to confirm that the ACF field which is selected is showing the correct results by using this code:

    
    <?php 
    $cat = get_field('associated_collection', 'category_2414'); 
    echo '<p>'.($cat).'</p>'
    ?>
    

    Here, the asssociated_collection is my ACF field and category_2414 is the parent Woocommerce category. This code shows the correct ACF field that has been selected.

    Any ideas on what I might be missing or how I can achieve this?

    Thanks in advance for any help. Let me know if more clarity is needed to troubleshoot this.

  • Update
    I was able to get the categories to appear by using this code, but I would still like to find a way to group these categories together based on what ACF field (from the Select Field option) was chosen. With this code, it is appearing as:

    ACF Field A
    Woocommerce category name A

    ACF Field B
    Woocommerce cateogry name B

    ACF Field A
    Woocommerce category name C

    Where I would prefer it look like this:

    ACF Field A
    Woocommerce category name A
    Woocommerce category name C

    ACF Field B
    Woocommerce category name B

    
    <?php 
    
      $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => 0
      );
      $c = get_categories($args);
      $c_keep = array();
      foreach($c as  $cat){
        if (get_field('associated_collection', 'category_'.$cat->term_id)) {
          $c_keep[] = $cat; // forgot [] here
        }
      }
    
      foreach($c_keep as $cat){
        echo '<h1>'.get_field('associated_collection', 'category_'.$cat->term_id).'</h1>';
        echo '<p>'.$cat->name.'</p>';
      }
    
    ?>
    

    Any ideas on what I’m missing?

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.