forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountryWithWebsites.php
157 lines (137 loc) · 5.05 KB
/
CountryWithWebsites.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Customer country with website specified attribute source
*/
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
use Magento\Customer\Model\Config\Share;
use Magento\Customer\Model\Config\Share as CustomerShareConfig;
use Magento\Directory\Model\AllowedCountries;
use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
use Magento\Eav\Model\Entity\Attribute\Source\Table;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory as OptionCollectionFactory;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory as AttrubuteOptionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Request\Http;
use Magento\Customer\Api\CustomerRepositoryInterface;
/**
* Return allowed countries for specified website
*/
class CountryWithWebsites extends Table
{
/**
* @var CountryCollectionFactory
*/
private $countriesFactory;
/**
* @var AllowedCountries
*/
private $allowedCountriesReader;
/**
* @var array
*/
private $options;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var Share
*/
private $shareConfig;
/**
* @var Http
*/
private $request;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @param OptionCollectionFactory $attrOptionCollectionFactory
* @param AttrubuteOptionFactory $attrOptionFactory
* @param CountryCollectionFactory $countriesFactory
* @param AllowedCountries $allowedCountriesReader
* @param StoreManagerInterface $storeManager
* @param Share $shareConfig
* @param Http|null $request
* @param CustomerRepositoryInterface|null $customerRepository
*/
public function __construct(
OptionCollectionFactory $attrOptionCollectionFactory,
AttrubuteOptionFactory $attrOptionFactory,
CountryCollectionFactory $countriesFactory,
AllowedCountries $allowedCountriesReader,
StoreManagerInterface $storeManager,
CustomerShareConfig $shareConfig,
?Http $request = null,
?CustomerRepositoryInterface $customerRepository = null
) {
$this->countriesFactory = $countriesFactory;
$this->allowedCountriesReader = $allowedCountriesReader;
$this->storeManager = $storeManager;
$this->shareConfig = $shareConfig;
$this->request = $request
?? ObjectManager::getInstance()->get(Http::class);
$this->customerRepository = $customerRepository
?? ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
}
/**
* @inheritdoc
*/
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
if (!$this->options) {
$websiteIds = [];
if (!$this->shareConfig->isGlobalScope()) {
$allowedCountries = [];
foreach ($this->storeManager->getWebsites() as $website) {
$countries = $this->allowedCountriesReader
->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId());
$allowedCountries[] = $countries;
foreach ($countries as $countryCode) {
$websiteIds[$countryCode][] = $website->getId();
}
}
$allowedCountries = array_unique(array_merge([], ...$allowedCountries));
} else {
// Address can be added only for the allowed country list.
$websiteId = null;
$customerId = $this->request->getParam('parent_id') ?? null;
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
$websiteId = $customer->getWebsiteId();
}
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries(
ScopeInterface::SCOPE_WEBSITE,
$websiteId
);
}
$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;
}
/**
* Create Countries Collection with all countries
*
* @return CountryCollection
*/
private function createCountriesCollection()
{
return $this->countriesFactory->create();
}
}