I guess this is no big issue, but I'm pretty new to Craft Commerce :) I'm trying to code a dynamic template for the product variants page.

This is the scenario: In the shop overview you see every product and all there variants. If you click on a variant image a detail page opens, showing you detailed information about this variant.

How can I access data of the variant (like title, description, asset) dynamically? Based on what the user clicked on?

I've already tried these two answers:

How do I select a specific product variant by element/field/entry?

Display image asset of a variant

The thing is, I can't use lineItem because the variant is not added to the cart yet. And I didn't manage to get the specific variant over getSegment and the url.

1 Answer 1


If I understand correctly you can check for available variants, then output whatever detail you need for the items. Something like this should get you started:

{# Check there are variants #}
{% if product.variants|length %}

  {# Loop through the variants #}
  {%- for purchasable in product.variants -%}

    {# Check the variant is available #}
    {% if purchasable.stock <= 0 and purchasable.unlimitedStock == false %}

      {{ purchasable.description }}
      {{ purchasable.salePrice|commerceCurrency(cart.currency) }}

    {% endif %}
  {%- endfor -%}

  {% else %}
    {{ "Out of Stock"|t }}
  {% endif %}

{% endif %}

If you want to target only a specific variant you can refer to it directly using either its title (slug) or its variant ID, e.g.:

{% set variants = craft.commerce.variants.id(8376).first() %}

You can find more detail in the docs for the Variant Model and the docs for Craft Commerce Variants.

  • Thank you! This goes into the right direction, it outputs all variants. But is there a way do output one specific variant? Based on the slug or ID for example? I'm thinking of something like this {% set variantSlug = craft.request.getSegment(4) %}but I still only get the product (=all variants), not just a specific one. Commented Jul 24, 2017 at 17:57
  • Sure! I've updated my answer above. Commented Jul 25, 2017 at 8:21
  • Glad to hear it. If my answer helped then perhaps you can mark it as correct. Thanks. Commented Jul 25, 2017 at 8:30
  • Of course! Will do so Commented Jul 26, 2017 at 9:02

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.