<p><code>{% set products = craft.commerce.products.relatedTo(category) %}</code></p>

<p>...is the correct code for finding products related to a category <strong>IF</strong> category is actually a Category Model at this point - e.g. it is the auto populated <code>category</code> variable in a template set to the template for that category group.  I would check, in the catgeory group settings, what you're using for the Category URL formats and the Category Template settings.</p>

<p>It would be also very helpful to know what's the actual error you are getting?</p>

CC BY-SA 3.0
2016-11-07T23:32:20.803Z
Sorry but this made no sense to me due to lack of knowledge: "is the correct code for finding products related to a category IF category is actually a Category Model at this point - e.g. it is the auto populated category variable in a template set to the template for that category group." Not sure how to explain what I have done other than the above. It has "product-categories/{slug}" for the url format and it is pointing to the page with the code above in it for the template.
Stephanie Hider
CC BY-SA 3.0
2016-11-07T23:33:06.310Z
The error is a template code that says "xxx" is not a variable no matter what configuration I do from the code above.

Variable "standard" does not exist  or with a straight copy and paste: Variable "category" does not exist
Stephanie Hider
CC BY-SA 3.0
2016-11-07T23:38:40.140Z
Ok, I wasn't talking about 'standard' - use the exact line I posted, ie. `{% set products = craft.commerce.products.relatedTo(category) %}` and what exact error do you get?  That should work.
Jeremy Daalder
CC BY-SA 3.0
2016-11-08T00:28:55.610Z
Same as above I posted:
Template Error

Variable "category" does not exist
Stephanie Hider
CC BY-SA 3.0
2016-11-08T00:53:09.323Z
Ok, that means that Craft is not auto-populating the category variable in that template, which means there's something wrong in the setup of you category templates (or you're somehow over-writing the category variable in your template so it's no longer set at that point). Rather than a big QnA here, are you on Slack maybe?
Jeremy Daalder
CC BY-SA 3.0
2016-11-07T23:12:19.570Z
2016-11-07T23:12:19.570Z
Jeremy Daalder
<p>In this line here:</p>

<pre><code>{% set products = craft.commerce.products.relatedTo(standard) %}
</code></pre>

<p>You want to fetch all products related to the category 'standard' right? What I think you're missing is you haven't defined what your variable <code>standard</code> is, yet. It should refer to a category model with the slug 'standard', but if you haven't defined it that probably explains your errors. This should work:</p>

<pre><code>{# fetch the category standard from the relevant categories group #}
{% set standardCategory = craft.categories.group('productCategories').slug('standard').first %}
</code></pre>

<p>You can then fetch your products like so (because you're feeding 'relatedTo' a category model now):</p>

<pre><code>{% set products = craft.commerce.products.relatedTo(standardCategory) %}
</code></pre>

<p>Alternatively, if you want to loop through your categories and fetch their products, you could just do:</p>

<pre><code>{# fetch the categories in the category group #}
{% set categories = craft.categories.group('productCategories') %}

{# loop through them, fetching their respective products #}
{% for category in categories %}
    {% set products = craft.commerce.products.relatedTo(category) %}
    {% if products | length %}
        {{ category.title }}
        {# do whatever you need to do with the products #}
        {% for product in products %}
            {{ product.title }}
        {% endfor %}
    {% endif %}
{% endfor %}
</code></pre>

<p>It's not the most efficient way of doing it if products have more than one category but should get you going.</p>

CC BY-SA 3.0
2016-11-09T11:56:09.787Z
2016-11-09T11:56:09.787Z
Clive Portman
<p>New to Craft Commerce and doing something pretty template intensive. </p>

<p>I am following this <a href="https://craftcommerce.com/support/product-taxonomy" rel="nofollow noreferrer">https://craftcommerce.com/support/product-taxonomy</a>  and <a href="https://craftcommerce.com/support/products-related-to-category" rel="nofollow noreferrer">https://craftcommerce.com/support/products-related-to-category</a></p>

<p>My categories group handle "productCategories"
My categories field handle "whatFinish"
Categories so far are "metallic" and "standard"</p>

<p>This code returns a short list of both categories from the page above</p>

<pre><code>{% nav category in product.productDepartments %}
  &lt;li&gt;
    &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
    {% ifchildren %}
      &lt;ul&gt;
        {% children %}
      &lt;/ul&gt;
    {% endifchildren %}
  &lt;/li&gt;
{% endnav %}
</code></pre>

<p>When I use this code with any of a variety of options to replace(category) I get a template error with a variable issue.</p>

<pre><code>{# Fetch all of the products related to this category #}
{% set products = craft.commerce.products.relatedTo(category) %}

{# Output them #}
{% for product in products %}
    &lt;a href="{{ product.url }}"&gt;{{ product.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

<p>Examples things I have tried: </p>

<pre><code>          {# Fetch all of the products related to this category #}
{% set products = craft.commerce.products.relatedTo(standard) %}

{# Output them #}
{% for product in products %}
    &lt;a href="{{ product.url }}"&gt;{{ product.title }}&lt;/a&gt;
{% endfor %}


 {# Fetch all of the products related to this category #}
{% set products = craft.commerce.products('whatFinish').relatedTo(standard) %}

{# Output them #}
{% for product in products %}
    &lt;a href="{{ product.url }}"&gt;{{ product.title }}&lt;/a&gt;
{% endfor %}

{# Fetch all of the products related to this category #}
{% set products = craft.commerce.products('standard').relatedTo(whatFinish) %}

{# Output them #}
{% for product in products %}
    &lt;a href="{{ product.url }}"&gt;{{ product.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

<p>Before to get all of the products (not desired as I want just the ones form a specific category) my code worked fine: </p>

<pre><code>  &lt;div class="row "&gt;
{% for product in craft.commerce.products.limit(5).find() %}
&lt;div class="col-md-4 col-sm-4  col-xs-12"&gt;
    &lt;div class=" text-center"&gt;
        {% set image = product.slider.first() %} {% if image %}
        &lt;img alt="product.title" class="" src="{{ image.getUrl({ height: 250 }) }}"&gt; {% endif %}
        &lt;div class="title"&gt;
            &lt;h5 class="mb0"&gt;{% if product.url %}{{ product.link }}{% else %}{{ product.title }}{% endif %}&lt;/h5&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;!--end three col--&gt;
{% endfor %}
</code></pre>

<p>
</p>

<p>Do I need to bracket the category code with something I am not aware of? Sorry just completely lost here. I was directed to post form the support email. </p>

<p>Steph</p>

<categories><plugin-craftcommerce>
Displaying products from a category
# Displaying products from a category

(Asked by: username_0 on 2016-11-07)

New to Craft Commerce and doing something pretty template intensive. 
I am following this https://craftcommerce.com/support/product-taxonomy  and https://craftcommerce.com/support/products-related-to-category
My categories group handle "productCategories"
My categories field handle "whatFinish"
Categories so far are "metallic" and "standard"
This code returns a short list of both categories from the page above
{% nav category in product.productDepartments %}
  
    {{ category.title }}
    {% ifchildren %}
      
        {% children %}
      
    {% endifchildren %}
  
{% endnav %}

When I use this code with any of a variety of options to replace(category) I get a template error with a variable issue.
{# Fetch all of the products related to this category #}
{% set products = craft.commerce.products.relatedTo(category) %}

{# Output them #}
{% for product in products %}
    {{ product.title }}
{% endfor %}

Examples things I have tried: 
          {# Fetch all of the products related to this category #}
{% set products = craft.commerce.products.relatedTo(standard) %}

{# Output them #}
{% for product in products %}
    {{ product.title }}
{% endfor %}


 {# Fetch all of the products related to this category #}
{% set products = craft.commerce.products('whatFinish').relatedTo(standard) %}

{# Output them #}
{% for product in products %}
    {{ product.title }}
{% endfor %}

{# Fetch all of the products related to this category #}
{% set products = craft.commerce.products('standard').relatedTo(whatFinish) %}

{# Output them #}
{% for product in products %}
    {{ product.title }}
{% endfor %}

Before to get all of the products (not desired as I want just the ones form a specific category) my code worked fine: 
  
{% for product in craft.commerce.products.limit(5).find() %}

    
        {% set image = product.slider.first() %} {% if image %}
         {% endif %}
        
            {% if product.url %}{{ product.link }}{% else %}{{ product.title }}{% endif %}
        
    


{% endfor %}



Do I need to bracket the category code with something I am not aware of? Sorry just completely lost here. I was directed to post form the support email. 
Steph

**Tags:** categories, plugin-craftcommerce

---

## 2 answers

### Answer by username_1 on 2016-11-07. Score: 1 | Accepted answer

`{% set products = craft.commerce.products.relatedTo(category) %}`

...is the correct code for finding products related to a category **IF** category is actually a Category Model at this point - e.g. it is the auto populated `category` variable in a template set to the template for that category group. I would check, in the catgeory group settings, what you're using for the Category URL formats and the Category Template settings.

It would be also very helpful to know what's the actual error you are getting?

> **Comments:**
> **username_0**: Sorry but this made no sense to me due to lack of knowledge: "is the correct code for finding products related to a category IF category is actually a Category Model at this point - e.g. it is the auto populated category variable in a template set to the template for that category group." Not sure how to explain what I have done other than the above. It has "product-categories/{slug}" for the url format and it is pointing to the page with the code above in it for the template.  
> **username_0**: The error is a template code that says "xxx" is not a variable no matter what configuration I do from the code above.  Variable "standard" does not exist  or with a straight copy and paste: Variable "category" does not exist  
> **username_1**: Ok, I wasn't talking about 'standard' - use the exact line I posted, ie. `{% set products = craft.commerce.products.relatedTo(category) %}` and what exact error do you get?  That should work.  
> **username_0**: Same as above I posted: Template Error  Variable "category" does not exist  
> **username_1**: Ok, that means that Craft is not auto-populating the category variable in that template, which means there's something wrong in the setup of you category templates (or you're somehow over-writing the category variable in your template so it's no longer set at that point). Rather than a big QnA here, are you on Slack maybe?  

---

### Answer by username_2 on 2016-11-09. Score: 0

In this line here:
{% set products = craft.commerce.products.relatedTo(standard) %}

You want to fetch all products related to the category 'standard' right? What I think you're missing is you haven't defined what your variable standard is, yet. It should refer to a category model with the slug 'standard', but if you haven't defined it that probably explains your errors. This should work:
{# fetch the category standard from the relevant categories group #}
{% set standardCategory = craft.categories.group('productCategories').slug('standard').first %}

You can then fetch your products like so (because you're feeding 'relatedTo' a category model now):
{% set products = craft.commerce.products.relatedTo(standardCategory) %}

Alternatively, if you want to loop through your categories and fetch their products, you could just do:
{# fetch the categories in the category group #}
{% set categories = craft.categories.group('productCategories') %}

{# loop through them, fetching their respective products #}
{% for category in categories %}
    {% set products = craft.commerce.products.relatedTo(category) %}
    {% if products | length %}
        {{ category.title }}
        {# do whatever you need to do with the products #}
        {% for product in products %}
            {{ product.title }}
        {% endfor %}
    {% endif %}
{% endfor %}

It's not the most efficient way of doing it if products have more than one category but should get you going.


---

