Order item not displayed the same in receipt email when using cron

Created on 21 July 2024, 3 months ago
Updated 5 August 2024, 2 months ago

Describe your bug or feature request.

Default receipt email:

Receipt email sent when a cron task validate order with https://www.drupal.org/project/commerce_order_autovalidate , and some custom event subscriber reacts to the validation to resend a receipt email but this time showing that the order had been validated. And this time, the order item label is not displayed.

If I validate an order in admin interface, there is no problem, I deduce that it is an access check problem when using CRON with Drush or not because I also tried to trigger cron on admin/config/system/cron and then also the order item label was not displayed.

In my commerce-order-receipt.html.twig email I have:

{% 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 style="vertical-align: top;">
                <td>
                  {{ order_item.getQuantity|number_format }} x
                </td>
                <td>
                  <span>
                    {{ order_item|commerce_entity_render('summary') }}

The important line is the last one, I use it to be able to control how I render the order items, instead of the hardcoded span>{{ order_item.label }}</span> by default.

If a bug, provide steps to reproduce it from a clean install.

Put the following code into a hook_cron, loading an existing order.

  $order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load(374);
  $mail = \Drupal::service('commerce_order.order_receipt_mail');
  $mail->send($order, NULL, NULL, TRUE);

In the receipt email template, render the order items using:

              {% for order_item in order_entity.getItems %}
                {{ order_item|commerce_entity_render('default') }}
              {% endfor %}

Check that when sending the receipt at the end of the order checkout there is the order item label.
When sending using CRON, there is no order item label.

💬 Support request
Status

Fixed

Version

2.38

Component

Order

Created by

🇫🇷France Grimreaper France 🇫🇷

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Grimreaper
  • 🇫🇷France Grimreaper France 🇫🇷

    Digging up, it was the access on the product variation which was problematic, order item fields were ok.

    Having updated the permissions on my website to give anonymous users access to my product types fixed the problem.

    I keep the issue opened because the access check needs to be done on the order's customer and not depend on the current user during sending mail code execution.

  • Status changed to Fixed 3 months ago
  • 🇮🇱Israel jsacksick

    I think the problem here is simply cron that's ran as user 0 (anonymous).
    What's happening is that the order item access is determined by the access on the order. Considering cron is executed as user 0, this can't work.

    This is more like a support request than a bug report.

    For example, if you'd like to run your cron as user 1, you can do the following:

    $account_switcher = \Drupal::service('account_switcher');
    $account_switcher->switchTo(new UserSession(['uid' => 1]));
    // Run your code...
    // then call the switchBack() method.
    $account_switcher->switchBack();
    
  • 🇫🇷France Grimreaper France 🇫🇷

    Thanks for the quick feedback.

    I tried to switch account but with \Drupal::currentUser()->setAccount()..., I felt it was not the right way to do it, but forgot how to do it and what to search for.

    Thanks for the code snippet, as mentioned, updating my user permissions fixed it, but I will try it to ensure it is ok. I will use the order's customer. With changes in Core 10.3 allowing to remove user 1 the super admin right, I don't know if it is a long term solution :)

    But I still think, sending the receipt email should already be evaluated in the order's customer context.

  • 🇮🇱Israel jsacksick

    But I still think, sending the receipt email should already be evaluated in the order's customer context.

    What you're suggesting would imply that the order receipt should switch the active user to the order customer. I think that is "risky". Admins are allow to resend receipts from the admin and they should have sufficient rights for the receipt to be rendered without issues.

  • 🇫🇷France Grimreaper France 🇫🇷

    Ok, thanks for the feedback :)

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024