Event "Order paid" - tokens are empty

Created on 25 July 2023, over 1 year ago

Problem/Motivation

When the event Order Paid is triggered, the token with order_number, order_items and completed are empty

Steps to reproduce

Setup an Order paid event, and use the following tokens on the subject and body:
[commerce_order:order_number]
[commerce_order:order_items]

Nevertheless, this tokens always work:

[commerce_order:total_paid]
[commerce_order:uuid:value]

The order is always anonymous. There is no user registration.

When using Test function, all the tokens are ok (testing with Admin permissions)

What could be happening on this?

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇵🇹Portugal jrochate

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

Comments & Activities

  • Issue created by @jrochate
  • 🇹🇷Turkey rgnyldz

    I can confirm this. The user is anonymous if the website has Guest checkout enabled and user choses to checkout as guest. Thus no user related info is visible inside the mail like [commerce_order:uuid:value] , [current-user:display-name] or[commerce_order:billing_profile]

    After loging in with the same user (password reset) and make a new order, all the tokens are visible in the mail.

  • 🇺🇸United States morbus iff

    Likely related to https://www.drupal.org/project/commerce/issues/3401590 📌 Dispatch the ORDER_PAID event for free orders when OrderStorage is destroyed Active .

  • I am facing the same problem, except it's for the "Order placed" event.
    Some tokens are available ([commerce_order:total_price], [commerce_order:order_number], [commerce_order:placed], [commerce_order:mail], [commerce_order:order_items]) and others not :

    • [commerce_order:billing_profile]
    • [commerce_order:payment_gateway]
    • [commerce_order:shipments]

    As jrochate, if I use the test function it gives me the expected result, meanwhile the real event firing not.
    Everything is good in the default drupal order receipt sent to customer, but I cannot do what is excepted with this module and send a mail to the shop with all details.

  • 🇺🇦Ukraine marchuk.vitaliy Rivne, UA

    vmarchuk made their first commit to this issue’s fork.

  • 🇺🇦Ukraine marchuk.vitaliy Rivne, UA

    Instead of [commerce_order:billing_profile] you should use something like [commerce_order:billing_profile:entity:address] or something else that is in [commerce_order:billing_profile:entity:*]. The same for the [commerce_order:payment_gateway] and [commerce_order:shipments] tokens.
    I've tested the token replacement for the events Order Paid and Order Placed with Guest checkout enabled and everything works fine.
    All the tokens below work fine:
    [commerce_order:total_price]
    [commerce_order:order_number]
    [commerce_order:placed]
    [commerce_order:mail]
    [commerce_order:order_items]
    [commerce_order:billing_profile:entity:address]
    [commerce_order:payment_gateway:target_id]
    [commerce_order:shipments] - show admin link to shipment (not sure how this can be useful for anonymous users)

    I'd be happy to help if anyone is still having trouble replacing tokens.

  • 🇺🇦Ukraine chizh273

    I have reproduced this issue partly, the [commerce_order:order_number] and [commerce_order:placed] tokens are empty when the product price is 0.

    Steps to reproduce:

    1. create an email for the “Order paid“ event and use [commerce_order:order_number] and [commerce_order:placed] tokens in the body field
    2. create a product with 0 price (we need 0 price for \Drupal\commerce_order\Entity\Order::isPaid)
    3. add to the cart this product and complete the checkout
    4. you will receive email without [commerce_order:order_number] and [commerce_order:placed]

    The problem here is related to the \Drupal\commerce_order\OrderStorage because \Drupal\commerce_order\Event\OrderEvents::ORDER_PAID event is called in the doOrderPreSave method and at that time we don’t have filled the order_number and placed fields yet.

    These fields will be filled with \Drupal\commerce_order\EventSubscriber\TimestampEventSubscriber::onPlaceTransition and \Drupal\commerce_order\EventSubscriber\OrderNumberSubscriber::setOrderNumber on the commerce_order.place.pre_transition event and this event is dispatched before order saving.

    When I enabled the commerce_payment module and configured manual payment, the issue was gone.

    To fix this issue, we can add the doPostSave method to the OrderStorage and move the OrderEvents::ORDER_PAID event dispatching to this method. If the OrderEvents::ORDER_PAID is dispatched from the doPostSave method, we can be sure that commerce_order.place.pre_transition was finished and order_number and placed fields are filled. But I’m not sure if it’s a good idea to move OrderEvents::ORDER_PAID event dispatching to the doPostSave method.

Production build 0.71.5 2024