Display parent category with Loopers

Hi,

I’m going to display product parent category in WC Single layout with custom loopers but it returns “Uncategorized” as result.

I have used this code in functions.php:

/**
 * Parent category with Loopers
 */

add_filter('cs_looper_custom_parentcat', function($result, $args)

 { 
   $parent_cat=$args['parent_cat'];
   $cat_args= get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => 0]);

  $parentcat = $cat_args;
  return $parentcat;
  
}, 10, 2);

and placed this code in JSON window:
$parent_cat=$args['parent_cat'];

and on Text element I enabled Looper Consumer with One item.

Could you please guide me to debug the process?

Thanks in advance.
Omid

Hi Omid,

Thanks for reaching out.
You need to set the Looper Consumer to All to get all the values, otherwise, the loop will run once if set to One. I would also suggest you go through the following thread which may help you on this.

Thanks

Hi @tristup, Thanks for reply.

I modified code and now the function returns only the parent categories:

/**
 * Get parent category with Loopers
 */

add_filter('cs_looper_custom_parentcat', function($result, $args) 
 {
 
$category = get_queried_object();
 
  $cat_args = array(
    'hide_empty' => 1,
     'parent' => 0,
    'taxonomy' => 'product_cat'
    
  );
  
  $parentcats = get_categories($cat_args);
return $parentcats;

}, 10, 2);

I also added this JSON to looper but honestly I don’t know is it a true code or not:

{
    "parent_post": "{{dc:term:id}}"
}

When I set the Looper Consumer to All then all the parent categories appear on single product page while I need just to display the parent category related to product. Also when I set to One then it returns irrelevant category.

Hi @omid020,

Would you mind sharing your admin credentials so that we can check on your setup properly? To do that, please give us the following information in a Secure Note.

  • WordPress Login URL
  • Admin level username and password
  • Page URL or Layout Builder Name

You can find the Secure Note button at the bottom of your posts.

Thank you.

Hi @marc_a, Thanks for reply.

Because the website is not in English language, I think this screenshot will help:

Hi Omid,

I have seen your code and it will return all the Parent Category and that is why it is showing all those. If you are trying to show only current product Categories I would suggest you use the Current Post Terms option of Looper Provider as shown in the given screenshot.

Screenshot 2022-02-23 140300

If that is not what you want and trying to show only the parent category not the child categories related to this specific Product, you required complex customization of the code. The customization is beyond the scope of Theme Support, I would suggest you hire a developer who can assist you to do the customization or you can avail of our newly launched service called One, where we answer the questions beyond normal theme support.

Thanks

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.