Commerce currently works with Symfony Mailer, but the integration is limited. Effectively Commerce is still building the email, and Symfony Mailer is only transporting it. If we add an EmailBuilder for Commerce then we can use all of the features of Symfony Mailer:
Twig templates & variables, Mailer Policy (for addresses, subject, body), rendering, language switching, etc. This would pave the way for removing the corresponding code from Commerce.

The commerce template commerce-order-receipt.html.twig is quite long (150 lines) and is effectively rendered HTML. The natural approach in Symfony Mailer would be to use Drupal mechanisms more:

  • Put the CSS in a CSS file which is part of a library - hence easy to theme
  • Render the order entity using formatters to generate markup for fields
  • Use "Manage Display" settings to control which fields are displayed and their order
  • The template can then be short and simple, with only any wrapping text

Original summary

I have create a new policy for commerce order email. But when I type tokens or twig code does not converted to values of them. Typed as clear text. Is something that I have to enable? The other examples that the module have produce by installation, register email etc (that use tokens as I have see) works ok and transform tokens to values at sending emails.

Comments

tarasiadis created an issue. See original summary.

adamps’s picture

Status: Active » Closed (works as designed)

This is as expected because the commerce order email does not support tokens or twig. Currently commerce builds the email and this module is barely involved.

tarasiadis’s picture

If I’ll use template file for commerce order email in my theme folder can I use twig there and send mail with symfony?

adamps’s picture

Exactly right - at the moment you have to use commerce-order-receipt.html.twig. Hopefully eventually Commerce will be more tightly integrated with Symfony Mailer.

tarasiadis’s picture

OK understand but a simple help as I'm comfused and I have not get it work with my custom template.
I have to copy email.html.twig file from modules/contrib/symfony_mailer/templates to themes/custom/mytheme/templete as email--commerce--order-reciept.html.twig right? OR Am I wrong? As I cannot get it work.

I have to have installed symfony_mailer_bc or not to get custom html template to rendered through my twig template file?

tarasiadis’s picture

I have found it. To be able to make yout twig template file you have to setup a policy for commerce and sub-type order receipt and then the system can read the email--commerce--order-reciept.html.twig file. But know how can I set custom subject with tokens to include some custom important fields on subject?

adamps’s picture

Title: I cannot use tokens and twig at all » Make tokens available for commerce emails
Version: 1.0.0-alpha4 » 1.x-dev
Category: Support request » Feature request
Status: Closed (works as designed) » Active

I tried to answer in #4 but I guess I wasn't clear. The template variable are currently not available in Symfony Mailer. I changed this issue to a feature request to add them.

However there is a workaround: you can continue to use the template from commerce: commerce-order-receipt.html.twig. Delete the body element from your symfony mailer policy, as it would overwrite it.

tarasiadis’s picture

I have no body element at policy. I use custom twig file and works ok. The problem is how to access data (variables) from the order. How can I access order id? Some help to get it? Something like $body['order_entity']['values']['order_id'] ?

adamps’s picture

I keep trying to say it😃.

Use the template from commerce: commerce-order-receipt.html.twig

There will be no variables available in email--commerce--order-reciept.html.twig until this issue is fixed.

tarasiadis’s picture

Thanks AdamPS, you have right I can use template from commerce: commerce-order-receipt.html.twig

tarasiadis’s picture

Now how can I customize the subject of the receipt email? I need to add some variables from user field and order.

adamps’s picture

Title: Make tokens available for commerce emails » Make twig variables available for commerce emails

I don't think Commerce supports that - it is fixed in OrderReceiptMail.php

For this module I raised #3274491: Replace twig variables in subject. It would only work for Commerce emails after this issue is fixed also.

adamps’s picture

Title: Make twig variables available for commerce emails » Full integration with commerce module
Issue summary: View changes
adamps’s picture

Status: Active » Needs work
StatusFileSize
new2.22 KB

Work in progress patch

giuseppe87’s picture

It is not clear to me how use the commerce-order-receipt.html.twig:

I've tried to copy the twig in my custom theme, but the receipt mail is still rendered by modules/contrib/symfony_mailer/templates/email.html.twig
and inside the body the one from

modules/contrib/commerce/modules/order/templates/co=
mmerce-order-receipt.html.twig

I've tried also to follow #6 but I don't know if I used the correct setup.

I've added a "policy" for Commerce. The sub-type is a free text area, where I added "order receipt".

Still, the twig loaded is the default one.

How can get this working? Thansk

adamps’s picture

Yes they would be one inside the other, that's what I would expect.

The default email.html.twig has no effect because it's like this

{{ body }}

If you changed your email.html.twig then you can make email--commerce--order-reciept.html.twig as above to reset.

giuseppe87’s picture

Sorry, your answer is not clear to me.

I'd need to theme\edit the markup inside commerce-order-receipt.html.twig, but I can't get the override working.

If I override email.html.twig with email--commerce--order-reciept.html.twig I'd get only the

{{ body }}

var, so I can't get the desired result.

Is currently possible somehow to override
commerce-order-receipt.html.twig or somehow edit the html of that mail?

realgiucas’s picture

@Giuseppe87, I have exactly the same problem and was wondering if you have found a way to use and override the twig template

rgeerolf’s picture

I'm experiencing the same issue. It is indeed impossible to overwrite the template as explained in the commerce documentation. With this patch it is possible to apply a policy for the order receipt so we can use our own email-*.html.twig template, but only the body variable is available.

I was already using a custom email.twig.html template for all my mails and the commerce-order-receipt was being rendered in the body variable from the custom email (symfony_mailer) template. So my idea was to strip down the commerce-order-receipt template so I could still use the same markup from the other mails around it. However, this wasn't working since I cannot overwrite the contrib commerce-order-receipt template.

adamps’s picture

It is indeed impossible to overwrite the template

I'm fairly sure it's working on my test site. I suspect that you are seeing this bug #3280322: LegacyEmailBuilder calls hook_mail() too early causing problems about which theme is used for templates.

rgeerolf’s picture

@AdamPS Yes indeed, I was looking in the wrong issue. Overwriting the template in your base theme is indeed working by checking #3280322 and installing the latest dev. Thanks!

imclean’s picture

@tarasiadis #11,

Now how can I customize the subject of the receipt email?

See the Commerce issue #2924159: Provide a way to customize the order receipt email subject.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new0 bytes

This patch is nearly ready now, testing welcome.

It will allow customising the order receipt subject (even a different one per order type)

adamps’s picture

StatusFileSize
new9.21 KB
4kant’s picture

Just tried #24

After patching and when I try to import the new configuration at "/admin/config/system/mailer/import/commerce_order_type"

I get

"TypeError: Argument 2 passed to Drupal\symfony_mailer\Entity\MailerPolicy::import() must be of the type array, null given, called in /web/modules/contrib/symfony_mailer/modules/symfony_mailer_bc/src/Plugin/EmailBuilder/CommerceOrderEmailBuilder.php on line 76 in /web/modules/contrib/symfony_mailer/src/Entity/MailerPolicy.php on line 407 #0 /web/modules/contrib/symfony_mailer/modules/symfony_mailer_bc/src/Plugin/EmailBuilder/CommerceOrderEmailBuilder.php(76): Drupal\symfony_mailer\Entity\MailerPolicy::import('commerce_order_...', NULL)"

- Drupal Core 9.4.5
- Commerce 8.x-2.31

adamps’s picture

Status: Needs review » Needs work

Thanks @4kant I can see what needs changing and will load a new patch soon.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new1.05 KB
adamps’s picture

StatusFileSize
new9.53 KB
4kant’s picture

Thanks AdamPS - the error has gone now.

I had had a pretty handy HTML order confirmation mail sent to the customer, built by commerce-order-receipt.html.twig + email.html.twig (this was the default so far...)

Right now I configured a new email policy at admin/config/system/mailer. (btw.: Inside the order type I never get a policy combined with a subtype and therefore it never works). The new commerce order policy with subtyp "receipt" is working - I get the new subject. But the tokens don´t work - in my case "[commerce_order:order_number:value]".

How do I have to write or use templates now? And which tokens or variables do work (where can I find them?)

Thanks again!

adamps’s picture

Thanks for testing. True, there's not much documentation yet, sorry.

The template currently supports 2 twig variables, see CommerceOrderEmailBuilder, calls to setVariable():

order_number => $order->getOrderNumber()
'store' => $store->getName())

I could add in commerce_order like it was before, however I thought perhaps it isn't needed so much now and might confuse people. The order is already built into {body}, and it can be managed in "manage display" settings for the order type, display mode = email (so the default order type is configured at admin/commerce/config/order-types/default/edit/display/email).

adamps’s picture

In the new patch there is an update hook that will import default config for the body and subject.

alexdezark’s picture

Hi AdamPS,

When can we expect to have a fully functional beta version?

adamps’s picture

Assigned: Unassigned » adamps
perarg’s picture

Firstly, #31 cannot be applied due to lack of symfony_mailer_bc.install file. So #27 is applied without any error.

Secondly, I needed another email scenario in commerce. When an admin go into an order, go to Shipments tab, enter for example a tracking link and it is needed to resend a confirmation. A way to get it is to change in following file symfony_mailer\modules\symfony_mailer_bc\src\Plugin\EmailBuilder\CommerceEmailBuilder.php the line 24 to if ($message['key'] == 'order_receipt' || $message['key'] == 'shipment_confirmation')

Probably is not the preferred approach, but i want to notice here that maybe there are more 'keys' than only order_receipt for commerce.

adamps’s picture

StatusFileSize
new12.16 KB

Firstly, #31 cannot be applied due to lack of symfony_mailer_bc.install file. So #27 is applied without any error.

Done reroll

i want to notice here that maybe there are more 'keys' than only order_receipt for commerce.

Yes but they are not in the core commerce project, so they won't be part of this patch. They could be solved in a separate issue.

Commerce uses MailManager::mail() in a non-standard way as it sets $module to commerce but actually the module is commerce_order or something else. This module can't handle that yet however I have some ideas how to do it.

adamps’s picture

The problem with other keys like 'shipment_confirmation' should now be fixed.

adamps’s picture

adamps’s picture

Status: Needs review » Needs work

The last submitted patch, 38: symfony_mailer.commerce.3271421-38.patch, failed testing. View results

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new8.96 KB
new1.17 KB

Another reroll

  • AdamPS committed 02c1a536 on 1.x
    Issue #3271421 by AdamPS, tarasiadis, 4kant: Full integration with...
adamps’s picture

Status: Needs review » Fixed

  • AdamPS committed 9198f976 on 1.x
    Issue #3271421 by AdamPS: Full integration with commerce module (fix...
adamps’s picture

Assigned: adamps » Unassigned

  • AdamPS committed 775e6bb6 on 1.x
    Issue #3271421 by AdamPS: Full integration with commerce module (...
adamps’s picture

Sorry this issue was eventually fixed in 3 commits

adamps’s picture

Status: Fixed » Needs review
StatusFileSize
new6.23 KB

New patch fixes some missing config defaults

  • AdamPS committed 8f8f6455 on 1.x
    Issue #3271421 by AdamPS: Full integration with commerce module
    
adamps’s picture

Status: Needs review » Fixed

Sorry this issue was eventually fixed in 4 commits

geek-merlin’s picture

This is awesome progress! I hope i can soon-ish leverage and test this in a project.

There are some comments in CommerceOrderEmailBuilder code that Commerce Core may need some adjustments, too.
Does this mean that this integration conflicts with the existing integration?
Does this need additional followups for Commerce Core, or are some on the way?

+ * @todo Notes for adopting Symfony Mailer into commerce. It should be possible
+ * to remove the MailHandler service and classes such as OrderReceiptMail. The
+ * commerce_order_receipt template could be retired, switching instead to use
+ * email__commerce_order_type__receipt or by editing Mailer Policy for
+ * commerce_order_type.
adamps’s picture

@geek-merlin
This issue adds integration of Commerce and Symfony Mailer by adding code to Symfony Mailer. It's complete and working (as far as I know😃) without any further changes to Commerce.

Those comments relate to a theoretical future date when Commerce might decide to adopt Symfony Mailer by changing code in Commerce (after which the code added in this issue could be removed).

Status: Fixed » Closed (fixed)

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

jmee’s picture

Just applied the patch and find the changes for "Commerce order" to work pretty much as expected, awesome! Thanks for this patch!

I'm having issues trying to use the new "Commerce order" policy type, it seems that the email is using the commerce_order.html.twig template, which is the default entity display for the order entity.

My use case: I have a functioning template in my theme called commerce-order-receipt.html.twig and I was hoping to use a mailer policy to customize the BCC and Subject depending on the order type, but to continue using the Body from my custom template (ie. not add a body element to the policy).

In the theme debug:

 THEME HOOK: 'commerce_order' 
 FILE NAME SUGGESTIONS:
   * commerce-order--5--email.html.twig
   * commerce-order--5.html.twig
   * commerce-order--download--email.html.twig
   * commerce-order--download.html.twig
   * commerce-order--email.html.twig
   x commerce-order.html.twig

If I customize any of these templates, I'll be changing how the site displays orders. I also don't have access to the same variables as the commerce-order-receipt template.

This template is called by email.html.twig:

 THEME HOOK: 'email' 
 FILE NAME SUGGESTIONS:
   * email--commerce-order-type--resend-receipt--download.html.twig
   * email--commerce-order-type--resend-receipt.html.twig
   * email--commerce-order-type.html.twig
   x email.html.twig

Again, not really a feasible option.

It seems like the intended use case for this patch is new users of Commerce, so existing users of commerce and symfony_mailer may have a hard time taking advantage of the features. In order to use the Commerce order policy type, the body content *must* be added as a policy element and it is not possible to use a twig template -- is that correct ?

If so, I think there's a good argument to change this behaviour and instead fall back to the default commerce-order-receipt template

EDIT for clarity: applying this patch does break my existing order receipt emails, replacing it with the order entity template, so any commerce users who want to continue using the commerce-order-receipt template should not apply this patch.

martijn de wit’s picture

@jmee if you experiencing any new problems. Please open a new ticket as follow up.