1

in my confirmation e-mail i output all ordered items.

now i want to add a custom field (the article number) from a product, but it does not show up.

i show items like this:

{% for item in order.lineItems %} and then for example {{ item.qty }}.

But {{ item.articleNumber }} does not work.

i also tried

{% set product = craft.products() %}
{{ product.articleNumber }}

1 Answer 1

3

There are two ways of doing this: you can hook into the "live" product data using {{ item.purchasable.product.articleNumber }} -- and this is probably fine for the use-case of a confirmation email that presumably gets sent immediately. However, if you're outputting historic order data on the front end, or generating sales reports etc, then ideally you don't want to use the live data for this purpose - rather you want a static snapshot of the data as it was at the time of the order. That's what {{ item.snapshot }} is for, so I'd generally prefer that for safety.

However, out of the box, {{ item.snapshot.product }} doesn't include any custom fields. To get your custom fields saved to the snapshot you need to save them to the cart during your add-to-cart form submissions. You need an array field named options which you can then add the data to like this:

<input type="hidden" name="options[articleNumber]" value="123">

Then you can retrieve that in the order with {{ item.snapshot.options.articleNumber }}

A better explanation of snapshots: https://craftcms.stackexchange.com/a/35658/271

About line item options: https://craftcms.com/docs/commerce/3.x/orders-carts.html#line-item-options-and-notes

2
  • thanks. this worked. but haven't really got the snapshot working. can not find anything in the docs: craftcms.com/docs/commerce/3.x/…
    – Henrik
    Commented Mar 25, 2022 at 14:59
  • Sorry yes, I've edited the answer to explain further. Commented Mar 25, 2022 at 15:15

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.