Skip to content

Commit

Permalink
Merge pull request #26 from ThomasDaSilva/main
Browse files Browse the repository at this point in the history
add notification event for update order status
  • Loading branch information
zawaze committed Sep 21, 2023
2 parents 79c2326 + 1992fdc commit 8cc5ab5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<language>fr_FR</language>
<language>en_US</language>
</languages>
<version>2.0.1</version>
<version>2.0.2</version>
<authors>
<author>
<name>Thelia</name>
Expand Down
37 changes: 29 additions & 8 deletions EventListeners/SendConfirmationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
namespace CmCIC\EventListeners;

use CmCIC\CmCIC;
use Exception;
use Propel\Runtime\Exception\PropelException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Action\BaseAction;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Log\Tlog;

class SendConfirmationEmail implements EventSubscriberInterface
class SendConfirmationEmail extends BaseAction implements EventSubscriberInterface
{
protected EventDispatcherInterface $dispatcher;

Expand All @@ -31,9 +34,9 @@ public function __construct(EventDispatcherInterface $dispatcher)
/**
* @param OrderEvent $event
*
* @throws \Exception if the message cannot be loaded.
* @throws Exception if the message cannot be loaded.
*/
public function sendConfirmationEmail(OrderEvent $event)
public function sendConfirmationEmail(OrderEvent $event): void
{
if (CmCIC::getConfigValue('send_confirmation_message_only_if_paid')) {
// We send the order confirmation email only if the order is paid
Expand All @@ -46,26 +49,44 @@ public function sendConfirmationEmail(OrderEvent $event)

/**
* @param OrderEvent $event
* @throws \Propel\Runtime\Exception\PropelException
*
* @throws Exception if the message cannot be loaded.
*/
public function sendNotificationEmail(OrderEvent $event): void
{
if (CmCIC::getConfigValue('send_confirmation_message_only_if_paid')) {
// We send the order notification email only if the order is paid
$order = $event->getOrder();
if (!$order->isPaid() && $order->getPaymentModuleId() == CmCIC::getModuleId()) {
$event->stopPropagation();
}
}
}

/**
* @param OrderEvent $event
* @throws PropelException
*/
public function updateStatus(OrderEvent $event)
public function updateStatus(OrderEvent $event): void
{
$order = $event->getOrder();
if ($order->isPaid() && $order->getPaymentModuleId() == CmCIC::getModuleId()) {
// Send confirmation email if required.
if (CmCIC::getConfigValue('send_confirmation_message_only_if_paid')) {
$this->dispatcher->dispatch($event, TheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL);
$this->dispatcher->dispatch($event, TheliaEvents::ORDER_SEND_NOTIFICATION_EMAIL);
}

Tlog::getInstance()->debug("Confirmation email sent to customer " . $order->getCustomer()->getEmail());
}
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return array(
TheliaEvents::ORDER_UPDATE_STATUS => array("updateStatus", 100),
TheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL => array("sendConfirmationEmail", 129)
TheliaEvents::ORDER_UPDATE_STATUS => array("updateStatus", 128),
TheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL => array("sendConfirmationEmail", 129),
TheliaEvents::ORDER_SEND_NOTIFICATION_EMAIL => array("sendNotificationEmail", 129)
);
}
}

0 comments on commit 8cc5ab5

Please sign in to comment.