0

I use a simple custom module to send confirmation emails in Drupal 9 Commerce 2 for a custom order states.

I am basing my module off of the example shown in the handbook. The main module script resides in modules/custom/my_newmodule/src/EventSubscriber, its name is OrderProcessSubscriber.php and its code is as follows:

<?php

namespace Drupal\my_newmodule\EventSubscriber;

use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
    * Sends an email when the order transitions to Fulfillment.
    */
class OrderProcessSubscriber implements EventSubscriberInterface {

    use StringTranslationTrait;

    /**
    * The language manager.
    *
    * @var \Drupal\Core\Language\LanguageManagerInterface
    */
    protected $languageManager;

    /**
    * The mail manager.
    *
    * @var \Drupal\Core\Mail\MailManagerInterface
    */
    protected $mailManager;

    /**
    * Constructs a new OrderFulfillmentSubscriber object.
    *
    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
    *   The language manager.
    * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
    *   The mail manager.
    */
    public function __construct(
    LanguageManagerInterface $language_manager,
    MailManagerInterface $mail_manager
    ) {
    $this->languageManager = $language_manager;
    $this->mailManager = $mail_manager;
    }

    /**
    * {@inheritdoc}
    */
    public static function getSubscribedEvents() {
    $events = [
        'commerce_order.process.post_transition' => ['sendEmail', -100],
    ];
    return $events;
    }




    /**
    * Sends the email.
    *
    * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
    *   The transition event.
    */
    public function sendEmail(WorkflowTransitionEvent $event) {
    // Create the email.
    $order = $event->getEntity();
    $to = $order->getEmail();
    $myTime = $order->getPlacedTime();
    $myplaced = date("m-d-Y", $myTime);

    $params = [
        'from' => $order->getStore()->getEmail(),
        'subject' => $this->t(
        'Regarding your order [#@number]',
        ['@number' => $order->getOrderNumber()]
        ),
        'body' => ['#markup' => $this->t(
        '

Order Summary:<br><br>
Order: @number<br>
Order Date: @placed<br>
Total paid: @paid<br><br>
',
[
'@number' => $order->getOrderNumber(),
'@placed' => $myplaced,
'@paid' => $order->getTotalPaid(),
]
        )],
    ];

    // Set the language that will be used in translations.
    if ($customer = $order->getCustomer()) {
        $langcode = $customer->getPreferredLangcode();
    }
    else {
        $langcode = $this->languageManager->getDefaultLanguage()->getId();
    }

    // Send the email.
    $this->mailManager->mail('commerce', 'receipt', $to, $langcode, $params);
    }

}

I was able to add "Order number" to it, "Order date" and "Total paid" fields, but I cannot add an ordered items list. I tried everything I know, I even tried to implement some recursive function against $order->getItems() to get its fields with no luck. I don't even understand how to see the list of available fields in it. Function print_r gives me memory errors, increasing available memory in php.ini does not help. Looks like any debug info simply cannot fit in email.

How can I get a list of ordered items (Title, Unit price, Quantity, Total price and Image if possible for every order item) and use it in this confirmation letter?

And how can I debug such situations and be able to see content of the complicated variables and objects in Drupal?

PS: I also tried to use EntityTypeManagerInterface this way:

...
    * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
    *   The transition event.
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
    *   The transition event.
    */
    public function sendEmail(WorkflowTransitionEvent $event, EntityTypeManagerInterface $entity_type_manager) {
...

But with no luck either, I get this error:

TypeError: Drupal\my_newmodule\EventSubscriber\OrderProcessSubscriber::__construct(): Argument #1 ($entity_type_manager) must be of type Drupal\Core\Entity\EntityTypeManagerInterface, Drupal\language\ConfigurableLanguageManager given, called in /var/www/test/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 in Drupal\my_newmodule\EventSubscriber\OrderProcessSubscriber->__construct() (line 43 of modules/custom/my_newmodule/src/EventSubscriber/OrderProcessSubscriber.php).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.