Score:0

How do I print a view field content from Order field in the order receipt email?

tr flag

I have a view field on the Order type that displays content from a webform submission that is associated with the order via the Commerce Webform Order module. This view field successfully displays that view's content on the order details page for both admin and shopper.

I want that webform data to be displayed on the order receipt email. I have found that {{ order_entity|commerce_entity_render }} will render the order (the view field content, along with the admin comments, the products ordered, etc) in the email. That is far too much data, and some of it is a bit "sensitive" or non-sensical to the customer (such as the administrator order comments).

How can I display only the content of the field_registration_details field (the view field) in the order receipt email?

I tried setting up a custom display setting (called "email") for the order type, and I only have the view field visible in that display, but that doesn't seem to work (although I may not be calling that display correctly in the template code). I also tried adding the view field directly to the order receipt email template, but that displays nothing at all.

One other possibility I considered was trying to use {{ additional_order_fields }}, which is used in the commerce-order--admin.html.twig template file, but I am not sure exactly how I could leverage that in the email template.

Score:0
tr flag

EDIT: Actually, my issue is NOT resolved. It works when I click the "Resend Receipt" button in admin, but it does NOT work when the customer first places the order. I feel like this must be some sort of permissions error, but I already enabled the "View own webform submission" permission.

I also enabled "Anonymous" and "Authenticated" in Settings > Access > VIEW OWN SUBMISSIONS for this webform, but that made no difference either.

I then tried placing an order as admin, thinking that would clearly show whether it is a permissions issue. The order email for the admin user's order also did not include the content from the view. So it is apparently not a role/permissions issue either.

Anyone have any ideas on how to resolve?

=======================================

Original (not-working) fix:

Issue resolved. I installed the Twig Tweak module, and then added this to my order email template file:

{{ drupal_view('my_view_name', 'block_1', order_entity.getOrderId) }}

If I had used the Order Number as the contextual filter instead of the Order ID, then I believe the code would look like this:

{{ drupal_view('my_view_name', 'block_1', order_entity.getOrderNumber) }}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.