Skip to content

Commit

Permalink
MAGETWO-56936: [Backport][Github] Allowed countries for customer addr…
Browse files Browse the repository at this point in the history
…ess in admin using storeview configuration #2946
  • Loading branch information
Sergii Kovalenko committed Sep 12, 2016
1 parent 7edd1d1 commit 37d35ca
Show file tree
Hide file tree
Showing 14 changed files with 385 additions and 296 deletions.
24 changes: 22 additions & 2 deletions app/code/Magento/Customer/Model/Customer/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
*/
protected $loadedData;

/**
* @var CountryWithWebsites
*/
private $countryByWebsiteSource;

/**
* @var \Magento\Customer\Model\Config\Share
*/
private $shareConfig;

/**
* EAV attribute properties to fetch from meta storage
* @var array
Expand Down Expand Up @@ -218,21 +228,31 @@ protected function getAttributesMeta(Type $entityType)
}

/**
* Retrieve Country With Website options Source
* @deprecated
* @return CountryWithWebsites
*/
private function getCountryByWebsiteSource()
{
return ObjectManager::getInstance()->get(CountryWithWebsites::class);
if (!$this->countryByWebsiteSource) {
$this->countryByWebsiteSource = ObjectManager::getInstance()->get(CountryWithWebsites::class);
}

return $this->countryByWebsiteSource;
}

/**
* Retrive Customer Share Config
* @deprecated
* @return \Magento\Customer\Model\Config\Share
*/
private function getShareConfig()
{
return ObjectManager::getInstance()->get(\Magento\Customer\Model\Config\Share::class);
if (!$this->shareConfig) {
$this->shareConfig = ObjectManager::getInstance()->get(\Magento\Customer\Model\Config\Share::class);
}

return $this->shareConfig;
}

/**
Expand Down
62 changes: 62 additions & 0 deletions app/code/Magento/Customer/Model/Plugin/AllowedCountries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Customer\Model\Plugin;

use Magento\Customer\Model\Config\Share;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class AllowedCountries
*/
class AllowedCountries
{
/**
* @var \Magento\Customer\Model\Config\Share
*/
private $shareConfig;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param Share $share
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Share $share,
StoreManagerInterface $storeManager
) {
$this->shareConfig = $share;
$this->storeManager = $storeManager;
}

/**
* Retrieve all allowed countries or specific by scope depends on customer share setting
* @param \Magento\Directory\Model\AllowedCountries $subject
* @param string | null $filter
* @param string $scope
*/
public function beforeGetAllowedCountries(
\Magento\Directory\Model\AllowedCountries $subject,
$filter = null,
$scope = ScopeInterface::SCOPE_WEBSITE
) {
if ($this->shareConfig->isGlobalScope()) {
//Check if we have shared accounts - than merge all website allowed countries
$filter = array_map(function (WebsiteInterface $website) {
return $website->getId();
}, $this->storeManager->getWebsites());
$scope = ScopeInterface::SCOPE_WEBSITES;
}

return [$filter, $scope];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Country extends \Magento\Eav\Model\Entity\Attribute\Source\Table
*/
protected $_countriesFactory;

/**
* @var StoreResolverInterface
*/
private $storeResolver;

/**
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
Expand Down Expand Up @@ -59,11 +64,16 @@ protected function _createCountriesCollection()
}

/**
* Retrieve Store Resolver
* @deprecated
* @return StoreResolverInterface
*/
private function getStoreResolver()
{
return ObjectManager::getInstance()->get(StoreResolverInterface::class);
if (!$this->storeResolver) {
$this->storeResolver = ObjectManager::getInstance()->get(StoreResolverInterface::class);
}

return $this->storeResolver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
*/
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\Customer;
use Magento\Directory\Model\CountryHandlerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Customer\Model\Config\Share;
use Magento\Directory\Model\AllowedCountries;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Table
{
Expand All @@ -27,9 +23,9 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab
private $countriesFactory;

/**
* @var \Magento\Customer\Model\CountryHandler
* @var \Magento\Directory\Model\AllowedCountries
*/
private $countryHandler;
private $allowedCountriesReader;

/**
* @var array
Expand All @@ -42,23 +38,29 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab
private $storeManager;

/**
* CountryWithWebsites constructor.
* @var Share
*/
private $shareConfig;

/**
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
* @param \Magento\Directory\Model\CountryHandlerInterface $countryHandler
* @param AllowedCountries $allowedCountries
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory,
\Magento\Directory\Model\CountryHandlerInterface $countryHandler,
\Magento\Store\Model\StoreManagerInterface $storeManager
\Magento\Directory\Model\AllowedCountries $allowedCountries,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Customer\Model\Config\Share $shareConfig
) {
$this->countriesFactory = $countriesFactory;
$this->countryHandler = $countryHandler;
$this->allowedCountriesReader = $allowedCountries;
$this->storeManager = $storeManager;
$this->shareConfig = $shareConfig;
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
}

Expand All @@ -73,16 +75,21 @@ public function getAllOptions()
$allowedCountries = [];
$websiteIds = [];

foreach ($this->storeManager->getWebsites() as $website) {
$countries = $this->countryHandler
->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE, true);
$allowedCountries = array_merge($allowedCountries, $countries);
if (!$this->shareConfig->isGlobalScope()) {
foreach ($this->storeManager->getWebsites() as $website) {
$countries = $this->allowedCountriesReader
->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE);
$allowedCountries = array_merge($allowedCountries, $countries);

foreach ($countries as $countryCode) {
$websiteIds[$countryCode][] = $website->getId();
foreach ($countries as $countryCode) {
$websiteIds[$countryCode][] = $website->getId();
}
}
} else {
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries();
}


$this->options = $this->createCountriesCollection()
->addFieldToFilter('country_id', ['in' => $allowedCountries])
->toOptionArray();
Expand All @@ -98,6 +105,7 @@ public function getAllOptions()
}

/**
* Create Countries Collection with all countries
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
*/
private function createCountriesCollection()
Expand Down
39 changes: 30 additions & 9 deletions app/code/Magento/Customer/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
namespace Magento\Customer\Setup;

use Magento\Customer\Model\Customer;
use Magento\Directory\Model\CountryHandlerInterface;
use Magento\Directory\Model\AllowedCountries;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Encryption\Encryptor;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Setup\SetupInterface;
Expand Down Expand Up @@ -40,6 +41,16 @@ class UpgradeData implements UpgradeDataInterface
*/
protected $eavConfig;

/**
* @var AllowedCountries
*/
private $allowedCountriesReader;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param CustomerSetupFactory $customerSetupFactory
* @param IndexerRegistry $indexerRegistry
Expand Down Expand Up @@ -126,16 +137,25 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
*/
private function getStoreManager()
{
return \Magento\Framework\App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
if (!$this->storeManager) {
$this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
}

return $this->storeManager;
}

/**
* Retrieve Allowed Countries Reader
* @deprecated
* @return CountryHandlerInterface
* @return AllowedCountries
*/
private function getCountryHandler()
private function getAllowedCountriesReader()
{
return \Magento\Framework\App\ObjectManager::getInstance()->get(CountryHandlerInterface::class);
if (!$this->allowedCountriesReader) {
$this->allowedCountriesReader = ObjectManager::getInstance()->get(AllowedCountries::class);
}

return $this->allowedCountriesReader;
}

/**
Expand All @@ -162,20 +182,21 @@ private function mergeAllowedCountries(array $countries, array $newCountries, $i
*/
private function migrateStoresAllowedCountriesToWebsite(SetupInterface $setup)
{
$allowedCountriesReader = $this->getAllowedCountriesReader();
$allowedCountries = [];
//Process Websites
foreach ($this->getStoreManager()->getStores() as $store) {
$allowedCountries = $this->mergeAllowedCountries(
$allowedCountries,
$this->getCountryHandler()->getAllowedCountries($store->getId(), ScopeInterface::SCOPE_STORE),
$allowedCountriesReader->getAllowedCountries($store->getId(), ScopeInterface::SCOPE_STORE),
$store->getWebsiteId()
);
}
//Process stores
foreach ($this->getStoreManager()->getWebsites() as $website) {
$allowedCountries = $this->mergeAllowedCountries(
$allowedCountries,
$this->getCountryHandler()->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE),
$allowedCountriesReader->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE),
$website->getId()
);
}
Expand All @@ -186,7 +207,7 @@ private function migrateStoresAllowedCountriesToWebsite(SetupInterface $setup)
$connection->delete(
$setup->getTable('core_config_data'),
[
'path = ?' => CountryHandlerInterface::ALLOWED_COUNTRIES_PATH,
'path = ?' => AllowedCountries::ALLOWED_COUNTRIES_PATH,
'scope = ?' => ScopeInterface::SCOPE_STORES
]
);
Expand All @@ -199,7 +220,7 @@ private function migrateStoresAllowedCountriesToWebsite(SetupInterface $setup)
'value' => implode(',', $countries)
],
[
'path = ?' => CountryHandlerInterface::ALLOWED_COUNTRIES_PATH,
'path = ?' => AllowedCountries::ALLOWED_COUNTRIES_PATH,
'scope_id = ?' => $scopeId,
'scope = ?' => ScopeInterface::SCOPE_WEBSITES
]
Expand Down
Loading

0 comments on commit 37d35ca

Please sign in to comment.