Skip to content

Commit

Permalink
Merge pull request Smile-SA#13 from delyriand/fix_Smile-SA/magento2-m…
Browse files Browse the repository at this point in the history
…odule-seller#9

Fix Smile-SA/magento2-module-seller#9 - Display country information e…
  • Loading branch information
Fdec authored Sep 3, 2018
2 parents ab4c2b0 + 03b5694 commit 1e6877f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Model/CountryInformationAcquirer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @category Smile
* @package Smile\Map
* @author Maxime Leclercq <[email protected]>
* @copyright 2018 Smile
* @license Apache License Version 2.0
*/
namespace Smile\Map\Model;

use Magento\Framework\Exception\NoSuchEntityException;

/**
* Override default implementation of the Magento CountryInformationAcquirer
* for find country information even if it is not in the list of authorized countries.
*
* @category Smile
* @package Smile\Map
*/
class CountryInformationAcquirer extends \Magento\Directory\Model\CountryInformationAcquirer
{
/**
* CountryInformationAcquirer constructor.
*
* @param \Magento\Directory\Model\Data\CountryInformationFactory $countryInformationFactory
* @param \Magento\Directory\Model\Data\RegionInformationFactory $regionInformationFactory
* @param \Magento\Directory\Helper\Data $directoryHelper
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection
*/
public function __construct(
\Magento\Directory\Model\Data\CountryInformationFactory $countryInformationFactory,
\Magento\Directory\Model\Data\RegionInformationFactory $regionInformationFactory,
\Magento\Directory\Helper\Data $directoryHelper,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Directory\Model\ResourceModel\Country\Collection $countryCollection
) {
$this->countryCollection = $countryCollection;
parent::__construct($countryInformationFactory, $regionInformationFactory, $directoryHelper, $scopeConfig, $storeManager);
}

/**
* Get country and region information for the store.
*
* @param string $countryId Country Id
*
* @return \Magento\Directory\Api\Data\CountryInformationInterface
* @throws NoSuchEntityException
*/
public function getCountryInfo($countryId)
{
$store = $this->storeManager->getStore();
$storeLocale = $this->scopeConfig->getValue(
'general/locale/code',
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);

$countriesCollection = $this->countryCollection->addCountryIdFilter($countryId);
$regions = $this->directoryHelper->getRegionData();
$country = $countriesCollection->getFirstItem();

if (!$country || !$country->getId()) {
throw new NoSuchEntityException(
__(
'Requested country is not available.'
)
);
}
$countryInfo = $this->setCountryInfo($country, $regions, $storeLocale);

return $countryInfo;
}
}
7 changes: 7 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@
</argument>
</arguments>
</type>

<type name="Smile\Map\Model\AddressFormatter">
<arguments>
<!-- Override country info acquirer for find information even if it is not in the list of authorized countries -->
<argument name="countryInfo" xsi:type="object">Smile\Map\Model\CountryInformationAcquirer</argument>
</arguments>
</type>
</config>

0 comments on commit 1e6877f

Please sign in to comment.