I have created an "Event Registration" product type and order item type. In the order item type, I added fields for the registrant's name and job title (and a few other pieces of info), which are then displayed in the Add To Cart form. How can I print the submitted values for those fields in the commerce-order-receipt.html.twig?
Thanks!
Comments
Comment #2
hockey2112 commentedI am a step closer to a solution. I added the following code from commerce-checkout-order-summary.html.twig to my commerce-order-receipt.html.twig file:
This renders a summary that looks like this:
The Job Title data is the info I really need to print in the order email; the rest of the "summary" data needs to be removed. How can I modify the summary portion of the code to only pull that specific field into the order receipt email, and ignore the rest of the summary data shown above?
Comment #3
hockey2112 commentedFigured it out! Took a few steps:
{{ order_item|commerce_entity_render('summary') }}Comment #4
hockey2112 commentedOn a related note, I was able to print an Order field like this:
Comment #5
bojanz commentedThank you for documenting your journey :)
Comment #7
cayenne commentedThank you so much for posting this.
May I ask another question: did you just rewrite the twig file in place, move it to your theme, or, what I would like to do, override it in a custom module?
If the final choice, I'd LOVE a code snippet to make it work!
Comment #8
pozi commentedBest way to overwrite template from contrib module is to create your custom one.
Then in file custom_module_name.module you need to define hook - hook_theme()
Then follow: https://docs.drupalcommerce.org/commerce2/developer-guide/orders/customi...
Ideally would be not to replace contrib module template but extend twig block defined within.
Thanks to this you are sure that whenever update comes to contrib template you will not lose new features/updates.
Comment #9
lunk rat commentedThank you @hockey2112 for sharing your approach. Two other approaches that do not require viewfield on the Order item type:
{{ order_item|commerce_entity_render('default') }}, and configure fields/labels on the 'Default' view mode on your Manage Display of your Order Item type--or--create a new view mode for Order Item types (at/admin/structure/display-modes/view/add/commerce_order_item) call it "email" and in your twig template, configure your Order Item type's Manage Display to use the view mode, configure the fields on the view mode, then use{{ order_item|commerce_entity_render('email') }}in your commerce-order-receipt.html.twigYour view needs a contextual filter Order ID, which is passed in as
order_entity.getOrderNumberComment #10
nattsI'm so glad I finally found this thread!
I just needed one product attribute (with field machine name 'field_message') to be included in the item rows in the e-mail, so I adapted the template like this:
So if the order_item has a value for field_message, that value goes into a new third table column (if not then the cell is empty), with the fourth column now right-aligned with the total price as before.
And instead of putting this commerce-order-receipt.html.twig file in a new custom module, I just included it in my existing custom theme (in its templates directory), with the following in the THEMENAME.theme file:
Comment #11
tarasiadis commented#9 works very nice to my setup. Thanks
Comment #12
hockey2112 commentedI just used the method in #10 since I didn't want to go the Viewfield route for a single order item field. Worked great!
Comment #13
ganeshc commentedHow can I get and print SKU of product (order_item) in twig template?
Also how can I get purchase order number separately? Right now I am getting it in "Payment method" like this: Purchase Order# 654.
Comment #14
hockey2112 commented@ganeshc, This worked for me:
{{ order_item.getPurchasedEntity.getSku }}Does anyone know how I can print the order email value in the order receipt template? In other words, I want the customer's email address to appear in the template/email receipt.
Thanks
Comment #15
pankaj1390 commentedHow we can user custm field with order_item order_entity?
Comment #16
hockey2112 commented@lunk rat (re: #9)... I am trying to include a Viewfield from the Order type (not the Order Item type). I added this code to my email template file after installing Twig Tweak:
{{ drupal_view('league_registration_webform_data', 'block_1', order_entity.getOrderNumber) }}Unfortunately, it had no effect; nothing was displayed. Any ideas on how I can make that work?
----------------------------EDIT------------------------
I got it to work! I had to use "getOrderId" instead of "getOrderNumber", as shown below:
{{ drupal_view('league_registration_webform_data', 'block_1', order_entity.getOrderId) }}Comment #17
norwegian.blue commented@hockey2112
I printed the recipient email with:
{{ ' email address: @emailaddr'|t({'@emailaddr': order_entity.mail.value}) }}I needed a custom field from the product variation type (field_displayed_title)
{{ order_item.getPurchasedEntity.field_displayed_title.value }}worked in may case
Comment #18
terminator727 commentedhow i can make the same with the product image? my product image is a "field_image", i created a news display "Image" like the SKU display and this on my template: "order_item.getPurchasedEntity.getImage" but doesn't load any product images.. wrong way?
Comment #19
thalemn commentedTo add product images, my solution was to create a block of the commerce_order_item_table view, and add the block to commerce-order-receipt twig template using twig tweak. The tricky part was passing the order item ids arguments to the block. Here's the code in the twig email template that worked for me:
You can uncomment the termids to see that it's working as expected.
Hope this helps!
Comment #20
Drupal_hippie commented@thalemn
But there are no image fields to add in this block..
Fields there are as follows:
Order item: Title (Title)
Order item: Unit price (Unit price)
Order item: Quantity (Quantity)
Order item: Total price (Total price)
Available fields are as follows:
Contextual Links Global Display fields in a contextual links menu.
Update Custom text
Custom text Global Provide custom text or link.
Update Dropbutton
Dropbutton Global Display fields in a dropbutton.
Update View result counter
View result counter Global Displays the actual position of the view result
Update Adjustments
Adjustments Order item
Update Adjustments (adjustments:delta)
Adjustments (adjustments:delta) Order item
Update Changed
Changed Order item The time when the order item was last edited.
Update Created
Created Order item The time when the order item was created.
Update Data
Data Order item A serialized array of additional data.
Update ID
ID Order item
Update Locked
Locked Order item
Update Order
Order Order item The parent order.
Update Order item type
Order item type Order item
Update Overridden unit price
Overridden unit price Order item Whether the unit price is overridden.
Update Purchased entity
Purchased entity Order item The purchased entity.
Update Quantity
Quantity Order item The number of purchased units.
Update Quantity text field
Quantity text field Order item Adds a text field for editing the quantity.
Update Remove button
Remove button Order item Adds a button for removing the order item.
Update Rendered entity
Rendered entity Order item Renders an entity in a view mode.
Update Title
Title Order item The order item title.
Update Total price
Total price Order item The total price of the order item.
Update Total price (currency_code)
Total price (currency_code) Order item The total price of the order item.
Update Unit price
Unit price Order item The price of a single unit.
Update Unit price (currency_code)
Unit price (currency_code) Order item The price of a single unit.
Update Uses legacy adjustments
Uses legacy adjustments Order item
Update UUID
UUID Order item
__________________
Figured it out, I need to add additional relationship to the view.
Comment #21
thalemn commentedI added product variation relationship to my view to get the product variation images.
Here's the export of my view: