-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAGETWO-56941: CLONE - CLONE - [Github] Allowed countries for custome…
…r address in admin using storeview configuration #2946
- Loading branch information
Sergii Kovalenko
committed
Aug 19, 2016
1 parent
25b6600
commit 1b4ba9f
Showing
14 changed files
with
848 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
104 changes: 104 additions & 0 deletions
104
app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryByWebsite.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
/** | ||
* Customer country with website specified attribute source | ||
* | ||
* @author Magento Core Team <core@magentocommerce.com> | ||
*/ | ||
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source; | ||
|
||
use Magento\Customer\Api\Data\CustomerInterface; | ||
use Magento\Customer\Model\Customer; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
class CountryByWebsite extends \Magento\Eav\Model\Entity\Attribute\Source\Table | ||
{ | ||
/** | ||
* @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory | ||
*/ | ||
private $countriesFactory; | ||
|
||
/** | ||
* @var \Magento\Directory\Model\CountryHandler | ||
*/ | ||
private $countryHandler; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $options; | ||
|
||
/** | ||
* @var \Magento\Store\Model\StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @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 | ||
*/ | ||
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\CountryHandler $countryHandler, | ||
\Magento\Store\Model\StoreManagerInterface $storeManager | ||
) | ||
{ | ||
$this->countriesFactory = $countriesFactory; | ||
$this->countryHandler = $countryHandler; | ||
$this->storeManager = $storeManager; | ||
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory); | ||
} | ||
|
||
/** | ||
* Retrieve all options | ||
* | ||
* @return array | ||
*/ | ||
public function getAllOptions() | ||
{ | ||
if (!$this->options) { | ||
$allowedCountries = []; | ||
$websiteIds = []; | ||
|
||
foreach ($this->storeManager->getWebsites() as $website) { | ||
$countries = $this->countryHandler | ||
->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE, true); | ||
$allowedCountries = array_merge($allowedCountries, $countries); | ||
|
||
foreach ($countries as $countryCode) { | ||
$websiteIds[$countryCode][] = $website->getId(); | ||
} | ||
} | ||
|
||
$this->options = $this->createCountriesCollection() | ||
->addFieldToFilter('country_id', ['in' => $allowedCountries]) | ||
->toOptionArray(); | ||
|
||
foreach ($this->options as &$option) { | ||
if (isset($websiteIds[$option['value']])) { | ||
$option['website_ids'] = $websiteIds[$option['value']]; | ||
} | ||
} | ||
} | ||
|
||
return $this->options; | ||
} | ||
|
||
/** | ||
* @return \Magento\Directory\Model\ResourceModel\Country\Collection | ||
*/ | ||
private function createCountriesCollection() | ||
{ | ||
return $this->countriesFactory->create(); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.