1

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

2 Answers 2

1

{% 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?

5
  • 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. Nov 7, 2016 at 23:32
  • 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 Nov 7, 2016 at 23:33
  • 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. Nov 7, 2016 at 23:38
  • Same as above I posted: Template Error Variable "category" does not exist Nov 8, 2016 at 0:28
  • 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? Nov 8, 2016 at 0:53
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.