2

How would I access a custom product field in the cart. I did it with the snapshot attribute like so:

{# cart.html #}

{% for item in cart.lineItems %}
  {{ craft.commerce.products.id(item.snapshot.product.id).first().myCustomField }}
{% endfor %}

But is that the right way to do it?

5

You could set a variable to access the products model like so:

{% set product = item.purchasable.product %}

and then access the fields like normal {{ product.myCustomField }}

The snapshot holds the products fields as they were at the time the line item was added to the cart. So depending on how you're using the fields would dictate the method used.

You could also access the snapshot slightly cleaner using {{ item.snapshot.product.myCustomField }}

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.