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 Product Type’s settings, and it's set up with a URL format:

Craft Commerce Product Type edit screen

If you want to display your product elsewhere on your site you will need to set it to a variable, using a Product Query:

{% set myProduct = craft.products.one() %}

…you can then loop through this Product's Variants (or just use myProduct.getDefaultVariant() if it only has one):

{# This example is part of an "add-to-cart" form! #}

Configure your {{ myProduct.title }}:

<select name="purchasableIds[]">
    {% for purchasable in myProduct.getVariants() %}
        <option
            {% if not purchasable.getIsAvailable() %}disabled{% endif %}
            value="{{ purchasable.purchasableId }}">
            {{ purchasable.description }}
            {{ purchasable.price | currency(cart.paymentCurrency) }}
        </option>
    {% endfor %}
</select>

See the Commerce Documentation for more information about adding products to the cart!

0

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.