1

I'm pretty new to Craft and Craft Commerce and right now I'm struggling with the following: in the navigation I want a list item for every product type. This works with the following code:

{% for productType in craft.commerce.productTypes %}
    <li>{{ productType.name }}</li>
{% endfor %}

But how can I now link this product type to an collection template and show only products of this type?

And the second question is: If at least one product has the tag "new" I want a list item for this in the menu as well. I created a tag field for this. But how can I now check if there are products with this tag and can I link this to an collection template as well?

Thanks for your help!

1

Well as always with Craft there are many ways to skin this particular cat.

A simple approach would be to have a catalogue template (either just one for all product types or separate ones for each product type if you want to vary the format significantly).

Assuming just one template for your various catalogues, this could live in your templates folder at e.g. _site/product-catalogue.html

You could set up a route in Settings->Routes, such that

*/catalogue pointed to /_site/product-catalogue

this template would then load whenever someone went to e.g.

yoursite.com/monitors/catalogue

In there you could query for all products of that type and display them:

{% set segment_1 = craft.request.getSegment(1) %}
{% set products = craft.commerce.products.type(segment_1).find() %}

...and proceed from there any way you like, really, in terms of display.

In terms of building you nav:

{% for productType in craft.commerce.productTypes %}
    <li><a href="/{{ productType.handle }}/catalogue">{{ productType.name }}</a></li>
{% endfor %}

(Another approach would be to add all the products to a category, and then use category fields and a category template to display your product list).

I haven't used tags much, and I am not sure I'm 100% on what you're asking (do you want to highlight sections with new products, or create an extra element in your nav for these?)....but in essense you'd probably have to search for products in that category and look for a tag of New. Here's some sketch code:

{% for productType in craft.commerce.productTypes %}
   results = search for entries with this type that have the tag New
   if the length of results > 0
      <li><a href="/productType.handle/catalogue">*** NEW *** {{ productType.name }}</a></li>
   else
      <li><a href="/productType.handle/catalogue">{{ productType.name }}</a></li>
   endif
{% endfor %}

Craft docs on doing the search are here: https://craftcms.com/docs/searching

  • Thank you very much for your help. I will dive into routes to get this working! I want to have an additional link in the navigation when a certain tag is used for a product. I solved it like that: {% if 'New' in craft.tags.group('productTags') %} <li><a href="/collection/tags/new">New In</a></li> {% endif %} – obs Jul 7 '16 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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