0

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. How can I print the submitted values for those fields in the commerce-order-receipt.html.twig?

  • I tried {{ order_item.field_job_itle }}, but that results in an error ("Exception: Object of type Drupal\Core\Field\FieldItemList cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 476 of /home/mysite/public_html/core/lib/Drupal/Core/Template/TwigExtension.php).") – hockey2112 Jul 22 at 19:23
0

Figured it out! Took a few steps:

  1. I created a View of type: Order Item. Added my custom fields to that view (Job Title, etc).
  2. Added a Contextual Filter "Order item: ID". Provide Default Value: Content ID from URL.
  3. I created a Viewfield on each of my Order Item Types, using the View I created in the previous step.
  4. I went to /admin/commerce/config/order-item-types/default/edit/display and hid all the fields except for the Viewfield. Repeat for the other Order Item Types. In commerce-order-receipt.html.twig, add this code (which was found in commerce-checkout-order-summary.html.twig):

    {{ order_item|commerce_entity_render('summary') }}
    
  5. Style the output as you like in the receipt twig. Here is the pertinent portion of mine in case anyone finds it helpful:

            <tr>
              <td>
                {% block order_items %}
                <table style="padding-top: 15px; padding-bottom:15px; width: 100%">
                  <tbody style="text-align: left;">
                  {% for order_item in order_entity.getItems %}
                  <tr>
                    <td>
                      <span style="font-size: 15px;">
                {{ order_item.getQuantity|number_format }} x 
                <span style="font-weight: bold;">{{ order_item.label }}</span>
                <span style="float: right;">{{ order_item.getTotalPrice|commerce_price_format }}</span>
              </span>
              <br>
              {{ order_item|commerce_entity_render('summary') }}
              <br><br><hr><br>
                    </td>
                  </tr>
                  {% endfor %}
                  </tbody>
                </table>
                {% endblock %}
              </td>
            </tr>
    
  6. Clear cache, and that's it!

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.