1

I am using Craft Commerce for the first time and am trying to dynamically generate a dropdown menu that contains links to each product type. I thought the best way to accomplish this would be by using a parameter in the URL i.e.

myurl.com/commerce/products?cat=books

I would be able to get the handle of the selected product type by using:

craft.commerce.products.type(productType).find()

I have two roadblocks that are preventing me from implementing this:

  1. How do I get an array of all the product types so I can build the dropdown?

  2. How do I get the name of a product type once I have the handle so I can display a Header at the top of the page based on the selected product type?

Also, is there a better way of implementing similar functionality that I am simply overlooking?

Thanks!

1

You can get all product types within your templates with:

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

There is no way to get a specific product type within templates at the moment, but I have added to the list. In the meantime you would need to look over all and find the one you want as above.

  • 1
    Awesome, this is working great! Thank you! The only issue I have at this point is how to get the handle from productType, productType.handle doesn't seem to be working and the product type page in the docs isn't completed yet. – StrangeDevs Mar 21 '16 at 18:45
1

To expand on Luke's answer, you might try something like:

<form action="">
  <select name="availableProductTypes">
     {% for theType in craft.commerce.productTypes %}
        <option value="{{theType}}">{{theType.name}}</option>
     {% endfor %}
  </select>
  <input type="submit" value="Show">
</form>

Then you can show what was selected in the dropdown:

{% if craft.request.getParam('availableProductTypes') %}

 {% set theType = craft.request.getParam('availableProductTypes') %}

 <ul>
  {% for product in craft.commerce.products.type(theType).find() %}
   <li>{{product}}</li>
  {% else %}
   <li>No products found.</li>
 {% endfor %}
 </ul>

{% endif %}

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.