Really struggling with this (perhaps my brain is tired), but how would I get the image of a product for each entry in the cart screen in Craft Commerce?


This actually depends where the image is stored - if the asset (image) field is on the product, and is called myImage, then as per Brad's example you'd use:


...BUT if it's in rather a field on the variant you would instead reference it as:


(This is because lineitem refers to a variant....in the case of a product without variants it references the implicitly created defaultVariant).

  • Somehow I knew either you or Luke would correct me. :) – Brad Bell Jul 26 '16 at 23:59

You'd access the product images just like you would any other Assets field in Craft.

Assuming you've already got the cart, and your Assets field is called productImages, something like this should work:

{% for item in cart.lineItems %}
    <p>SKU: {{ item.sku }}</p>

    {% for image in item.purchasable.product.productImages %}
        <li><img src="{{ image.url }}">
    {% endfor %}

{% endfor %}
  • Yeah, I had tried that but I get the following error. Craft\Commerce_LineItemModel and its behaviors do not have a method or closure named "productImage". I had assumed it would be as simple as fetching a normal asset, but everything I have tried fails. – Terry Upton Jul 25 '16 at 22:33
  • That's why I said "and your Assets field is called productImages". :) What's the name of the Assets field you're storing the product images in? – Brad Bell Jul 25 '16 at 22:41
  • productImage. I have changed the code to reflect my asset field name. – Terry Upton Jul 25 '16 at 22:43
  • When I do a dump. {{ dump(item) }} I can't even see any reference to the productImage field. :-( – Terry Upton Jul 25 '16 at 22:51
  • Updated my answer. And actually tested it this time. :) – Brad Bell Jul 25 '16 at 23:13

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.