2

My shop has one product. As such there isn't much point in me having a page displaying all products.

When I paste in my template code for my single product into Cart Commerce's main index template I get the error Variable "product" does not exist on the following code:

{% for purchasable in product.variants %}

    <option {% if purchasable.stock <= 0 and purchasable.unlimitedStock == false %}disabled {% endif %} value="{{ purchasable.purchasableId }}">{{ purchasable.description }} {{ purchasable.price|currency(cart.currency) }}</option>

{% endfor %}

Why is this being thrown up and how do I remedy this?

1

product will only be available by default if you're viewing the template set in the CP under that products type settings:

enter image description here

If you want to display your product elsewhere on your site you will need to set it to a variable like so:

{% set myProduct = craft.commerce.products.first() %}

you can then loop through this product's variants

{% for purchasable in myProduct.variants %}

    <option {% if purchasable.stock <= 0 and purchasable.unlimitedStock == false %}disabled {% endif %} value="{{ purchasable.purchasableId }}">{{ purchasable.description }} {{ purchasable.price|currency(cart.currency) }}</option>

{% endfor %}

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.