Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-07T23:32:20.803Z
Answers.Text: 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.
Answers.UserDisplayName: Stephanie Hider
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-07T23:33:06.310Z
Answers.Text: 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
Answers.UserDisplayName: Stephanie Hider
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-07T23:38:40.140Z
Answers.Text: 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.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-08T00:28:55.610Z
Answers.Text: Same as above I posted:
  Template Error
  
  Variable "category" does not exist
Answers.UserDisplayName: Stephanie Hider
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-08T00:53:09.323Z
Answers.Text: 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?
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-07T23:12:19.570Z
Answers.LastActivityDate: 2016-11-07T23:12:19.570Z
Answers.UserDisplayName: Jeremy Daalder
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-09T11:56:09.787Z
Answers.LastActivityDate: 2016-11-09T11:56:09.787Z
Answers.UserDisplayName: Clive Portman
Body: <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>
  
Tags: <categories><plugin-craftcommerce>
Title: Displaying products from a category
ThreadText: # 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 %}
    <li>
      <a href="{{ category.url }}">{{ category.title }}</a>
      {% ifchildren %}
        <ul>
          {% children %}
        </ul>
      {% endifchildren %}
    </li>
  {% 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 %}
      <a href="{{ product.url }}">{{ product.title }}</a>
  {% 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 %}
      <a href="{{ product.url }}">{{ product.title }}</a>
  {% endfor %}
  
   {# Fetch all of the products related to this category #}
  {% set products = craft.commerce.products('whatFinish').relatedTo(standard) %}
  
  {# Output them #}
  {% for product in products %}
      <a href="{{ product.url }}">{{ product.title }}</a>
  {% endfor %}
  
  {# Fetch all of the products related to this category #}
  {% set products = craft.commerce.products('standard').relatedTo(whatFinish) %}
  
  {# Output them #}
  {% for product in products %}
      <a href="{{ product.url }}">{{ product.title }}</a>
  {% endfor %}
  
  ```
  
  Before to get all of the products (not desired as I want just the ones form a specific category) my code worked fine:
  
  ```
    <div class="row ">
  {% for product in craft.commerce.products.limit(5).find() %}
  <div class="col-md-4 col-sm-4  col-xs-12">
      <div class=" text-center">
          {% set image = product.slider.first() %} {% if image %}
          <img alt="product.title" class="" src="{{ image.getUrl({ height: 250 }) }}"> {% endif %}
          <div class="title">
              <h5 class="mb0">{% if product.url %}{{ product.link }}{% else %}{{ product.title }}{% endif %}</h5>
          </div>
      </div>
  </div>
  <!--end three col-->
  {% 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.
  
  
  ---
  
  