Skip to content

Commit 97cfd94

Browse files
szsanyiSanyi
andauthored
Ema 153 allow event trigger if not default template (#66)
Co-authored-by: Sanyi <[email protected]>
1 parent 1f73508 commit 97cfd94

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Release notes:
22
==============
33

4+
2.0.13
5+
-------
6+
* Fix
7+
* Fixing email trigger, if not default template has been selected
8+
49
2.0.12
510
-------
611
* Fix

Events/SenderBuilderPlugin.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use Emartech\Emarsys\Helper\Json;
99
use Emartech\Emarsys\Model\EventFactory as EmarsysEventFactory;
1010
use Emartech\Emarsys\Model\EventRepository;
11+
use Magento\Config\Model\ResourceModel\Config as ConfigResource;
12+
use Magento\Framework\App\Config\Initial\Reader as InitialConfigReader;
1113
use Magento\Framework\Exception\AlreadyExistsException;
14+
use Magento\Framework\Exception\LocalizedException;
1215
use Magento\Sales\Model\AbstractModel;
1316
use Magento\Sales\Model\Order;
1417
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
@@ -48,30 +51,51 @@ class SenderBuilderPlugin
4851
*/
4952
private $customerHelper;
5053

54+
/**
55+
* @var ConfigResource
56+
*/
57+
private $configResource;
58+
59+
/**
60+
* @var InitialConfigReader
61+
*/
62+
private $initialConfigReader;
63+
64+
/**
65+
* @var array
66+
*/
67+
private $initialConfig = [];
68+
5169
/**
5270
* SenderBuilderPlugin constructor.
5371
*
5472
* @param ConfigReader $configReader
73+
* @param InitialConfigReader $initialConfigReader
5574
* @param EmarsysEventFactory $eventFactory
5675
* @param EventRepository $eventRepository
5776
* @param Json $json
5877
* @param CustomerHelper $customerHelper
78+
* @param ConfigResource $configResource
5979
* @param LoggerInterface $logger
6080
*/
6181
public function __construct(
6282
ConfigReader $configReader,
83+
InitialConfigReader $initialConfigReader,
6384
EmarsysEventFactory $eventFactory,
6485
EventRepository $eventRepository,
6586
Json $json,
6687
CustomerHelper $customerHelper,
88+
ConfigResource $configResource,
6789
LoggerInterface $logger
6890
) {
6991
$this->configReader = $configReader;
92+
$this->initialConfigReader = $initialConfigReader;
7093
$this->eventFactory = $eventFactory;
7194
$this->eventRepository = $eventRepository;
7295
$this->json = $json;
7396
$this->logger = $logger;
7497
$this->customerHelper = $customerHelper;
98+
$this->configResource = $configResource;
7599
}
76100

77101
/**
@@ -121,7 +145,7 @@ public function aroundSend(SenderBuilder $senderBuilder, callable $proceed)
121145
$this->saveEvent(
122146
$this->websiteId,
123147
$storeId,
124-
$templateContainer->getTemplateId(),
148+
$this->getOriginalTemplateId($templateContainer->getTemplateId()),
125149
$data['order']['entity_id'],
126150
$data
127151
);
@@ -272,4 +296,60 @@ private function saveEvent(int $websiteId, int $storeId, string $type, int $enti
272296

273297
$this->eventRepository->save($eventModel);
274298
}
299+
300+
/**
301+
* Get config path by template ID
302+
*
303+
* @param int $templateId
304+
*
305+
* @return string
306+
* @throws LocalizedException
307+
*/
308+
private function getConfigPathByTemplateId(string $templateId): string
309+
{
310+
$select = $this->configResource->getConnection()->select()
311+
->from($this->configResource->getMainTable(), ['path'])
312+
->where('value = ?', $templateId)
313+
->where('path like ?', '%template')
314+
->limit(1);
315+
316+
return (string) $this->configResource->getConnection()->fetchOne($select);
317+
}
318+
319+
/**
320+
* Get original template ID
321+
*
322+
* @param string $templateId
323+
*
324+
* @return string
325+
* @throws LocalizedException
326+
*/
327+
private function getOriginalTemplateId(string $templateId): string
328+
{
329+
if (!is_numeric($templateId)) {
330+
return $templateId;
331+
}
332+
333+
$configPath = $this->getConfigPathByTemplateId($templateId);
334+
if (!$configPath) {
335+
return $templateId;
336+
}
337+
338+
if (empty($this->initialConfig)) {
339+
$this->initialConfig = $this->initialConfigReader->read();
340+
}
341+
342+
if (isset($this->initialConfig['data']['default'])) {
343+
$configValue = $this->initialConfig['data']['default'];
344+
foreach (explode('/', $configPath) as $key) {
345+
$configValue = $configValue[$key] ?? [];
346+
}
347+
348+
if (!empty($configValue) && is_string($configValue)) {
349+
return (string) $configValue;
350+
}
351+
}
352+
353+
return $templateId;
354+
}
275355
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"php": ">7.0.1"
77
},
88
"type": "magento2-module",
9-
"version": "2.0.12",
9+
"version": "2.0.13",
1010
"autoload": {
1111
"files": [
1212
"registration.php"

etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22

33
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
4-
<module name="Emartech_Emarsys" setup_version="2.0.12">
4+
<module name="Emartech_Emarsys" setup_version="2.0.13">
55
<sequence>
66
<module name="Magento_Catalog" />
77
<module name="Magento_Inventory" />

0 commit comments

Comments
 (0)