|
8 | 8 | use Emartech\Emarsys\Helper\Json;
|
9 | 9 | use Emartech\Emarsys\Model\EventFactory as EmarsysEventFactory;
|
10 | 10 | use Emartech\Emarsys\Model\EventRepository;
|
| 11 | +use Magento\Config\Model\ResourceModel\Config as ConfigResource; |
| 12 | +use Magento\Framework\App\Config\Initial\Reader as InitialConfigReader; |
11 | 13 | use Magento\Framework\Exception\AlreadyExistsException;
|
| 14 | +use Magento\Framework\Exception\LocalizedException; |
12 | 15 | use Magento\Sales\Model\AbstractModel;
|
13 | 16 | use Magento\Sales\Model\Order;
|
14 | 17 | use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
|
@@ -48,30 +51,51 @@ class SenderBuilderPlugin
|
48 | 51 | */
|
49 | 52 | private $customerHelper;
|
50 | 53 |
|
| 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 | + |
51 | 69 | /**
|
52 | 70 | * SenderBuilderPlugin constructor.
|
53 | 71 | *
|
54 | 72 | * @param ConfigReader $configReader
|
| 73 | + * @param InitialConfigReader $initialConfigReader |
55 | 74 | * @param EmarsysEventFactory $eventFactory
|
56 | 75 | * @param EventRepository $eventRepository
|
57 | 76 | * @param Json $json
|
58 | 77 | * @param CustomerHelper $customerHelper
|
| 78 | + * @param ConfigResource $configResource |
59 | 79 | * @param LoggerInterface $logger
|
60 | 80 | */
|
61 | 81 | public function __construct(
|
62 | 82 | ConfigReader $configReader,
|
| 83 | + InitialConfigReader $initialConfigReader, |
63 | 84 | EmarsysEventFactory $eventFactory,
|
64 | 85 | EventRepository $eventRepository,
|
65 | 86 | Json $json,
|
66 | 87 | CustomerHelper $customerHelper,
|
| 88 | + ConfigResource $configResource, |
67 | 89 | LoggerInterface $logger
|
68 | 90 | ) {
|
69 | 91 | $this->configReader = $configReader;
|
| 92 | + $this->initialConfigReader = $initialConfigReader; |
70 | 93 | $this->eventFactory = $eventFactory;
|
71 | 94 | $this->eventRepository = $eventRepository;
|
72 | 95 | $this->json = $json;
|
73 | 96 | $this->logger = $logger;
|
74 | 97 | $this->customerHelper = $customerHelper;
|
| 98 | + $this->configResource = $configResource; |
75 | 99 | }
|
76 | 100 |
|
77 | 101 | /**
|
@@ -121,7 +145,7 @@ public function aroundSend(SenderBuilder $senderBuilder, callable $proceed)
|
121 | 145 | $this->saveEvent(
|
122 | 146 | $this->websiteId,
|
123 | 147 | $storeId,
|
124 |
| - $templateContainer->getTemplateId(), |
| 148 | + $this->getOriginalTemplateId($templateContainer->getTemplateId()), |
125 | 149 | $data['order']['entity_id'],
|
126 | 150 | $data
|
127 | 151 | );
|
@@ -272,4 +296,60 @@ private function saveEvent(int $websiteId, int $storeId, string $type, int $enti
|
272 | 296 |
|
273 | 297 | $this->eventRepository->save($eventModel);
|
274 | 298 | }
|
| 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 | + } |
275 | 355 | }
|
0 commit comments