Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ema 153 allow event trigger if not default template #66

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Release notes:
==============

2.0.13
-------
* Fix
* Fixing email trigger, if not default template has been selected

2.0.12
-------
* Fix
Expand Down
82 changes: 81 additions & 1 deletion Events/SenderBuilderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"php": ">7.0.1"
},
"type": "magento2-module",
"version": "2.0.12",
"version": "2.0.13",
"autoload": {
"files": [
"registration.php"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Emartech_Emarsys" setup_version="2.0.12">
<module name="Emartech_Emarsys" setup_version="2.0.13">
<sequence>
<module name="Magento_Catalog" />
<module name="Magento_Inventory" />
Expand Down
Loading