Skip to content

Commit de09ded

Browse files
committed
MC-42486: Region ID and Country ID values and not properly converted in Email templates
- Added the dynamic template variables for region and country in order to fetch these variables in email templates.
1 parent ed2ffad commit de09ded

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

app/code/Magento/Email/Model/AbstractTemplate.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\Store\Model\ScopeInterface;
1616
use Magento\Store\Model\Store;
1717
use Magento\MediaStorage\Helper\File\Storage\Database;
18+
use \Magento\Directory\Api\CountryInformationAcquirerInterface;
19+
use \Magento\Directory\Model\RegionFactory;
1820

1921
/**
2022
* Template model class.
@@ -58,6 +60,16 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
5860
*/
5961
const XML_PATH_DESIGN_EMAIL_LOGO_HEIGHT = 'design/email/logo_height';
6062

63+
/**
64+
* Email country id
65+
*/
66+
const XML_PATH_GENERAL_STORE_INFORMATION_COUNTRY_ID = 'general/store_information/country_id';
67+
68+
/**
69+
* Email region id
70+
*/
71+
const XML_PATH_GENERAL_STORE_INFORMATION_REGION_ID = 'general/store_information/region_id';
72+
6173
/**
6274
* Configuration of design package for template
6375
*
@@ -165,6 +177,16 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
165177
*/
166178
private $urlModel;
167179

180+
/**
181+
* @var \Magento\Directory\Api\CountryInformationAcquirerInterface
182+
*/
183+
protected $countryInformationAcquirerInterface;
184+
185+
/**
186+
* @var \Magento\Directory\Model\RegionFactory
187+
*/
188+
protected $regionFactory;
189+
168190
/**
169191
* @var Database
170192
*/
@@ -183,6 +205,8 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
183205
* @param \Magento\Email\Model\TemplateFactory $templateFactory
184206
* @param \Magento\Framework\Filter\FilterManager $filterManager
185207
* @param \Magento\Framework\UrlInterface $urlModel
208+
* @param CountryInformationAcquirerInterface $countryInformationAcquirerInterface
209+
* @param RegionFactory $regionFactory
186210
* @param array $data
187211
* @param Database $fileStorageDatabase
188212
*
@@ -201,6 +225,8 @@ public function __construct(
201225
\Magento\Email\Model\TemplateFactory $templateFactory,
202226
\Magento\Framework\Filter\FilterManager $filterManager,
203227
\Magento\Framework\UrlInterface $urlModel,
228+
CountryInformationAcquirerInterface $countryInformationAcquirerInterface,
229+
RegionFactory $regionFactory,
204230
array $data = [],
205231
Database $fileStorageDatabase = null
206232
) {
@@ -216,6 +242,8 @@ public function __construct(
216242
$this->templateFactory = $templateFactory;
217243
$this->filterManager = $filterManager;
218244
$this->urlModel = $urlModel;
245+
$this->countryInformationAcquirerInterface = $countryInformationAcquirerInterface;
246+
$this->regionFactory = $regionFactory;
219247
$this->fileStorageDatabase = $fileStorageDatabase ?:
220248
\Magento\Framework\App\ObjectManager::getInstance()->get(Database::class);
221249
parent::__construct($context, $registry, null, null, $data);
@@ -512,6 +540,12 @@ protected function addEmailVariables($variables, $storeId)
512540
$store
513541
);
514542
}
543+
if (!isset($variables['store_country'])) {
544+
$variables['store_country'] = $this->getCountryName($store);
545+
}
546+
if (!isset($variables['store_region'])) {
547+
$variables['store_region'] = $this->getRegionName($store);
548+
}
515549
// If template is text mode, don't include styles
516550
if (!$this->isPlain() && !isset($variables['template_styles'])) {
517551
$variables['template_styles'] = $this->getTemplateStyles();
@@ -773,4 +807,46 @@ public function getUrl(Store $store, $route = '', $params = [])
773807
}
774808
return $url->getUrl($route, $params);
775809
}
810+
811+
/**
812+
* Get country name for the specified store.
813+
*
814+
* @param Store $store
815+
* @return string
816+
*/
817+
protected function getCountryName($store)
818+
{
819+
$countryName = '';
820+
$store = $this->storeManager->getStore($store);
821+
$countryId = $this->scopeConfig->getValue(
822+
self::XML_PATH_GENERAL_STORE_INFORMATION_COUNTRY_ID,
823+
ScopeInterface::SCOPE_STORE,
824+
$store
825+
);
826+
if($countryId) {
827+
$countryName = $this->countryInformationAcquirerInterface->getCountryInfo($countryId)->getFullNameLocale();
828+
}
829+
return $countryName;
830+
}
831+
832+
/**
833+
* Get region name for the specified store.
834+
*
835+
* @param Store $store
836+
* @return string
837+
*/
838+
protected function getRegionName($store)
839+
{
840+
$regionName = '';
841+
$store = $this->storeManager->getStore($store);
842+
$regionId = $this->scopeConfig->getValue(
843+
self::XML_PATH_GENERAL_STORE_INFORMATION_REGION_ID,
844+
ScopeInterface::SCOPE_STORE,
845+
$store
846+
);
847+
if($regionId) {
848+
$regionName = $this->regionFactory->create()->load($regionId)->getName();
849+
}
850+
return $regionName;
851+
}
776852
}

app/code/Magento/Email/Model/BackendTemplate.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class BackendTemplate extends Template
3535
* @param \Magento\Email\Model\TemplateFactory $templateFactory
3636
* @param \Magento\Framework\Filter\FilterManager $filterManager
3737
* @param \Magento\Framework\UrlInterface $urlModel
38+
* @param \Magento\Directory\Api\CountryInformationAcquirerInterface $countryInformationAcquirerInterface
39+
* @param \Magento\Directory\Model\RegionFactory $regionFactory
3840
* @param \Magento\Email\Model\Template\FilterFactory $filterFactory
3941
* @param \Magento\Config\Model\Config\Structure $structure
4042
* @param array $data
@@ -55,6 +57,8 @@ public function __construct(
5557
\Magento\Email\Model\TemplateFactory $templateFactory,
5658
\Magento\Framework\Filter\FilterManager $filterManager,
5759
\Magento\Framework\UrlInterface $urlModel,
60+
\Magento\Directory\Api\CountryInformationAcquirerInterface $countryInformationAcquirerInterface,
61+
\Magento\Directory\Model\RegionFactory $regionFactory,
5862
\Magento\Email\Model\Template\FilterFactory $filterFactory,
5963
\Magento\Config\Model\Config\Structure $structure,
6064
array $data = [],
@@ -74,6 +78,8 @@ public function __construct(
7478
$templateFactory,
7579
$filterManager,
7680
$urlModel,
81+
$countryInformationAcquirerInterface,
82+
$regionFactory,
7783
$filterFactory,
7884
$data,
7985
$serializer

app/code/Magento/Email/Model/Template.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
118118
* @param TemplateFactory $templateFactory
119119
* @param \Magento\Framework\Filter\FilterManager $filterManager
120120
* @param \Magento\Framework\UrlInterface $urlModel
121+
* @param \Magento\Directory\Api\CountryInformationAcquirerInterface $countryInformationAcquirerInterface
122+
* @param \Magento\Directory\Model\RegionFactory $regionFactory
121123
* @param Template\FilterFactory $filterFactory
122124
* @param array $data
123125
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
@@ -138,6 +140,8 @@ public function __construct(
138140
\Magento\Email\Model\TemplateFactory $templateFactory,
139141
\Magento\Framework\Filter\FilterManager $filterManager,
140142
\Magento\Framework\UrlInterface $urlModel,
143+
\Magento\Directory\Api\CountryInformationAcquirerInterface $countryInformationAcquirerInterface,
144+
\Magento\Directory\Model\RegionFactory $regionFactory,
141145
\Magento\Email\Model\Template\FilterFactory $filterFactory,
142146
array $data = [],
143147
\Magento\Framework\Serialize\Serializer\Json $serializer = null
@@ -158,6 +162,8 @@ public function __construct(
158162
$templateFactory,
159163
$filterManager,
160164
$urlModel,
165+
$countryInformationAcquirerInterface,
166+
$regionFactory,
161167
$data
162168
);
163169
}

app/code/Magento/Email/view/frontend/email/footer.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
-->
77
<!--@subject {{trans "Footer"}} @-->
88
<!--@vars {
9-
"var store.frontend_name":"Store Name"
9+
"var store.frontend_name":"Store Name",
10+
"var store_country":"Country Name",
11+
"var store_region":"Region Name"
1012
} @-->
1113

1214
<!-- End Content -->

app/code/Magento/Email/view/frontend/email/header.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"var logo_alt":"Email Logo Alt Text",
1111
"var logo_height":"Email Logo Image Height",
1212
"var logo_width":"Email Logo Image Width",
13-
"var template_styles|raw":"Template CSS"
13+
"var template_styles|raw":"Template CSS",
14+
"var store_country":"Country Name",
15+
"var store_region":"Region Name"
1416
} @-->
1517

1618
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

app/design/frontend/Magento/luma/Magento_Email/email/footer.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"var url_customer_service":"Customer Service URL",
1212
"var store_phone":"Store Phone",
1313
"var store_hours":"Store Hours",
14-
"var store.formatted_address|raw":"Store Address"
14+
"var store.formatted_address|raw":"Store Address",
15+
"var store_country":"Country Name",
16+
"var store_region":"Region Name"
1517
} @-->
1618

1719
<!-- End Content -->

0 commit comments

Comments
 (0)