Provide a way to customize the order receipt email subject

Created on 16 November 2017, almost 7 years ago
Updated 12 August 2023, over 1 year ago

The DC2x deployment experience can be improved by adding site builder configurable Order Receipt email - subject header - and - from header - customization capabilities.

It seems like this can be implemented with an additional field for the order receipt email subject line in the store entity, viewed in configuration at:

{site}/admin/commerce/config/store-types/online/edit/form-display

and edited by the site builder in associated admin UI at:

{site}/store/*/edit

The field can include @variables for placement of the store-name and the order-number in the subject line - (@storename and @ordernumber), and support in the function - sendOrderReceipt(WorkflowTransitionEvent $event) - within:

{site}/modules/commerce/modules/order/src/EventSubscriber/OrderReceiptSubscriber.php

-

At present, the sendOrderReceipt() function uses a hard-coded email subject header text string when assembling the email subject header (see code excerpt).


    $params = [
      'headers' => [
        'Content-Type' => 'text/html',
      ],
      'from' => $order->getStore()->getEmail(),
      'subject' => $this->t('Order #@number confirmed', ['@number' => $order->getOrderNumber()]),
      'order' => $order,
    ];

Instead that string could be retrieved from a field in the store entity which can include the @variables in it. Of course the store entity subject field can include help text regarding the @variables. The "from" header could be similarly enhanced to provide a:

t( '"@storename" <@from>', ... )

construct to build the from header in the style ' "store name" '.

-

At present I've built a small module that implements a simplified version of this, although it doesn't include use of an additional store entity field for subject string.

Feature request
Status

Fixed

Version

2.0

Component

Order

Created by

🇺🇸United States websiteworkspace

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States DamienMcKenna NH, USA

    Shouldn't the update script set existing emails to have the old subject of 'Order #@number confirmed'? Also, would it be worth skipping the $this->token->replace() piece if the subject is empty?

        /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
        $order_type = $this->orderTypeStorage->load($order->bundle());
        $subject = $order_type->getReceiptSubject();
        if (!empty($subject)) {
          $subject = $this->token->replace($subject, [
            'commerce_order' => $order,
          ]);
        }
        // Provide a default value if the subject line was blank.
        if (empty($subject)) {
          $subject = $this->t('Order #@number confirmed', ['@number' => $order->getOrderNumber()]);
        }
    
  • 🇺🇸United States DamienMcKenna NH, USA

    I read through the rest of the changes and understand the direction that was being taken, so the update script works fine.

    Here's the minor code change from #17, along with a minor tidying.

  • 🇮🇱Israel jsacksick

    I don't think this will make it into core TBH. If we start introducing a setting for that, then I guess we should add 50 other settings :).

    For site builders not comfortable writing little code to override the order receipt subject, Commerce Email can be used instead of the default core order receipt email.

    The Commerce Email module also lets you customize the content of the email as well.

  • 🇺🇸United States DamienMcKenna NH, USA

    The decision on what to include vs not include in Commerce is a bit confusing. I would wager the overwhelming majority of sites want emails created as part of the commerce transaction process, so why not have it fully functioning out of the box, including the ability to modify the email subject (this issue)? OTOH the vast majority of sites don't need the ability to create separate stores, but that functionality is built in and mandatory.

  • 🇺🇸United States SocialNicheGuru

    I assume that you would either use the patch in this issue or "Commerce Email" module not both.

  • Status changed to Fixed over 1 year ago
  • 🇮🇱Israel jsacksick

    Decided to go ahead and commit a slightly modified version of the patch from comment #18. I actually wondered about getting the token service from the container for people that decorated the Order receipt mail service... But, this alone wouldn't work, so this could potentially be a BC even though I believe people decorating the order receipt mail service shouldn't really extend the base class but rather just implement the interface.

  • 🇮🇱Israel jsacksick

    Note that I got rid of the post update function which is unnecessary.

    • jsacksick committed deed7007 on 8.x-2.x
      Issue #2924159 followup by jsacksick: Properly access the profile view...
    • jsacksick committed 796a8689 on 3.0.x
      Issue #2924159 followup by jsacksick: Properly access the profile view...
  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica

    @jsacksick trying to translate the subject I just saw that the field was introduced as "string" in the config schema. but must be "label" to be translatable. That's an impediment for multilingual commerce projects. I think it's quite clearly wrong?

    Could you perhaps take a look at 🐛 Commerce Order config value receiptSubject ("Subject for the order receipt email") is not translatable Fixed and if the change is clear and fine, merge that?

    Thank you in advance! :)

Production build 0.71.5 2024