diff --git a/CHANGELOG.md b/CHANGELOG.md index 1230704..51b40e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Release notes: ============== +2.0.13 +------- +* Fix + * Fixing email trigger, if not default template has been selected + 2.0.12 ------- * Fix diff --git a/Events/SenderBuilderPlugin.php b/Events/SenderBuilderPlugin.php index 7cb4e1f..7bbf1a9 100644 --- a/Events/SenderBuilderPlugin.php +++ b/Events/SenderBuilderPlugin.php @@ -8,7 +8,10 @@ use Emartech\Emarsys\Helper\Json; use Emartech\Emarsys\Model\EventFactory as EmarsysEventFactory; use Emartech\Emarsys\Model\EventRepository; +use Magento\Config\Model\ResourceModel\Config as ConfigResource; +use Magento\Framework\App\Config\Initial\Reader as InitialConfigReader; use Magento\Framework\Exception\AlreadyExistsException; +use Magento\Framework\Exception\LocalizedException; use Magento\Sales\Model\AbstractModel; use Magento\Sales\Model\Order; use Magento\Sales\Model\Order\Email\Container\OrderIdentity; @@ -48,30 +51,51 @@ class SenderBuilderPlugin */ private $customerHelper; + /** + * @var ConfigResource + */ + private $configResource; + + /** + * @var InitialConfigReader + */ + private $initialConfigReader; + + /** + * @var array + */ + private $initialConfig = []; + /** * SenderBuilderPlugin constructor. * * @param ConfigReader $configReader + * @param InitialConfigReader $initialConfigReader * @param EmarsysEventFactory $eventFactory * @param EventRepository $eventRepository * @param Json $json * @param CustomerHelper $customerHelper + * @param ConfigResource $configResource * @param LoggerInterface $logger */ public function __construct( ConfigReader $configReader, + InitialConfigReader $initialConfigReader, EmarsysEventFactory $eventFactory, EventRepository $eventRepository, Json $json, CustomerHelper $customerHelper, + ConfigResource $configResource, LoggerInterface $logger ) { $this->configReader = $configReader; + $this->initialConfigReader = $initialConfigReader; $this->eventFactory = $eventFactory; $this->eventRepository = $eventRepository; $this->json = $json; $this->logger = $logger; $this->customerHelper = $customerHelper; + $this->configResource = $configResource; } /** @@ -121,7 +145,7 @@ public function aroundSend(SenderBuilder $senderBuilder, callable $proceed) $this->saveEvent( $this->websiteId, $storeId, - $templateContainer->getTemplateId(), + $this->getOriginalTemplateId($templateContainer->getTemplateId()), $data['order']['entity_id'], $data ); @@ -272,4 +296,60 @@ private function saveEvent(int $websiteId, int $storeId, string $type, int $enti $this->eventRepository->save($eventModel); } + + /** + * Get config path by template ID + * + * @param int $templateId + * + * @return string + * @throws LocalizedException + */ + private function getConfigPathByTemplateId(string $templateId): string + { + $select = $this->configResource->getConnection()->select() + ->from($this->configResource->getMainTable(), ['path']) + ->where('value = ?', $templateId) + ->where('path like ?', '%template') + ->limit(1); + + return (string) $this->configResource->getConnection()->fetchOne($select); + } + + /** + * Get original template ID + * + * @param string $templateId + * + * @return string + * @throws LocalizedException + */ + private function getOriginalTemplateId(string $templateId): string + { + if (!is_numeric($templateId)) { + return $templateId; + } + + $configPath = $this->getConfigPathByTemplateId($templateId); + if (!$configPath) { + return $templateId; + } + + if (empty($this->initialConfig)) { + $this->initialConfig = $this->initialConfigReader->read(); + } + + if (isset($this->initialConfig['data']['default'])) { + $configValue = $this->initialConfig['data']['default']; + foreach (explode('/', $configPath) as $key) { + $configValue = $configValue[$key] ?? []; + } + + if (!empty($configValue) && is_string($configValue)) { + return (string) $configValue; + } + } + + return $templateId; + } } diff --git a/composer.json b/composer.json index 53ef918..bde21ae 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "php": ">7.0.1" }, "type": "magento2-module", - "version": "2.0.12", + "version": "2.0.13", "autoload": { "files": [ "registration.php" diff --git a/etc/module.xml b/etc/module.xml index 817e505..4f51670 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,7 @@ - +