Created
February 24, 2021 14:19
-
-
Save mileandra/240e33a5c710a23de92ad10ef1edd9ac to your computer and use it in GitHub Desktop.
Intercept Forgot password and create account emails for Vue Storefront
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Plugin/Magento/Email/Model/AbstractTemplatePlugin.php | |
namespace VENDOR\MODULE\Plugin\Magento\Email\Model; | |
use Magento\Store\Model\ScopeInterface; | |
use Magento\Store\Model\Store; | |
class AbstractTemplatePlugin | |
{ | |
const VS_URL_XPATH = 'web/unsecure/vue_storefront_url'; | |
/** | |
* @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig | |
*/ | |
protected $scopeConfig; | |
/** | |
* @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface | |
*/ | |
protected $customerRepositoryInterface; | |
/** | |
* Constructor | |
* | |
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig | |
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface | |
*/ | |
public function __construct( | |
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | |
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface | |
) { | |
$this->scopeConfig = $scopeConfig; | |
$this->customerRepositoryInterface = $customerRepositoryInterface; | |
} | |
/** | |
* @param \Magento\Email\Model\AbstractTemplate $subject | |
* @param \Closure $proceed | |
* @param Store $store | |
* @param string $route | |
* @param array $params | |
* @return string | |
*/ | |
public function aroundGetUrl( | |
\Magento\Email\Model\AbstractTemplate $subject, | |
\Closure $proceed, | |
Store $store, | |
string $route, | |
array $params | |
) { | |
$intercepted = $this->getInterceptedUrls(); | |
if (in_array($route, array_keys($intercepted))) { | |
$newUrl = $this->getStorefrontUrl() . $intercepted[$route]; | |
if ($route == 'customer/account/createPassword/' && isset($params['_query']) && isset($params['_query']['id'])) { | |
$customerId = $params['_query']['id']; | |
$customer = $this->customerRepositoryInterface->getById($customerId); | |
if ($customer) { | |
$params['_query']['email'] = $customer->getEmail(); | |
unset($params['_query']['id']); | |
} | |
} | |
$generated = $proceed($store, $route, $params); | |
$urlArr = explode('?', $generated); | |
if (isset($urlArr[1])) { | |
$newUrl .= strpos($newUrl, '?') === false ? '?' : '&'; | |
$newUrl .= $urlArr[1]; | |
} | |
return $newUrl; | |
} | |
return $proceed($store, $route, $params); | |
} | |
private function getInterceptedUrls() { | |
return [ | |
'customer/account/createPassword/' => '?createPassword=1', | |
'customer/account/' => 'my-account' | |
]; | |
} | |
private function getStorefrontUrl() { | |
return $this->scopeConfig->getValue(self::VS_URL_XPATH, ScopeInterface::SCOPE_STORE); //from config, you can also hardcode | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- in etc/config.xml --> | |
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> | |
<default> | |
<web> | |
<unsecure> | |
<vue_storefront_url>http://localhost:3000/</vue_storefront_url> | |
</unsecure> | |
</web> | |
</default> | |
</config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- in etc/di.xml --> | |
<?xml version="1.0" encoding="UTF-8" ?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
<type name="Magento\Email\Model\AbstractTemplate"> | |
<plugin name="storefront-urls" type="VENDOR\MODULE\Plugin\Magento\Email\Model\AbstractTemplatePlugin" sortOrder="1" disabled="false" /> | |
</type> | |
</config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- etc/adminhtml/system.xml --> | |
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | |
<system> | |
<section id="web"> | |
<group id="unsecure"> | |
<field id="vue_storefront_url" translate="label comment" type="text" sortOrder="50" showInDefault="1" showInStore="1" showInWebsite="1"> | |
<label>Vue Storefront Url</label> | |
<comment><![CDATA[Store frontend url]]></comment> | |
</field> | |
</group> | |
</section> | |
</system> | |
</config> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment