Skip to content

Commit 6c4f37e

Browse files
Merge pull request #1069 from magento-engcom/2.1-develop-prs
[EngCom] Public Pull Requests - 2.1 - MAGETWO-67753: Fix a bug resulting in incorrect offsets with dynamic row drag-n-drop functionality #9376 - MAGETWO-67725: Backport of MAGETWO-59685 for Magento 2.1 - Checkout pages very slow #9364 - MAGETWO-67724: Backport of MAGETWO-60351 for Magento 2.1 - Unnecessary disabled payment methods #9365
2 parents 97a0832 + 0840c1e commit 6c4f37e

File tree

19 files changed

+949
-40
lines changed

19 files changed

+949
-40
lines changed

app/code/Magento/Checkout/Block/Cart/LayoutProcessor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public function process($jsLayout)
8585
'visible' => true,
8686
'formElement' => 'select',
8787
'label' => __('Country'),
88-
'options' => $this->countryCollection->loadByStore()->toOptionArray(),
88+
'options' => [],
8989
'value' => null
9090
],
9191
'region_id' => [
9292
'visible' => true,
9393
'formElement' => 'select',
9494
'label' => __('State/Province'),
95-
'options' => $this->regionCollection->load()->toOptionArray(),
95+
'options' => [],
9696
'value' => null
9797
],
9898
'postcode' => [
@@ -103,6 +103,13 @@ public function process($jsLayout)
103103
]
104104
];
105105

106+
if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
107+
$jsLayout['components']['checkoutProvider']['dictionaries'] = [
108+
'country_id' => $this->countryCollection->loadByStore()->toOptionArray(),
109+
'region_id' => $this->regionCollection->addAllowedCountriesFilter()->toOptionArray(),
110+
];
111+
}
112+
106113
if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
107114
['address-fieldsets']['children'])
108115
) {

app/code/Magento/Checkout/Block/Checkout/AttributeMerger.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ protected function getFieldConfig(
191191
'visible' => isset($additionalConfig['visible']) ? $additionalConfig['visible'] : true,
192192
];
193193

194+
if ($attributeCode === 'region_id' || $attributeCode === 'country_id') {
195+
unset($element['options']);
196+
$element['deps'] = [$providerName];
197+
$element['imports'] = [
198+
'initialOptions' => 'index = ' . $providerName . ':dictionaries.' . $attributeCode,
199+
'setOptions' => 'index = ' . $providerName . ':dictionaries.' . $attributeCode
200+
];
201+
}
202+
194203
if (isset($attributeConfig['value']) && $attributeConfig['value'] != null) {
195204
$element['value'] = $attributeConfig['value'];
196205
} elseif (isset($attributeConfig['default']) && $attributeConfig['default'] != null) {
@@ -340,18 +349,19 @@ protected function getCustomer()
340349
* @param string $attributeCode
341350
* @param array $attributeConfig
342351
* @return array
352+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
343353
*/
344354
protected function getFieldOptions($attributeCode, array $attributeConfig)
345355
{
346-
$options = isset($attributeConfig['options']) ? $attributeConfig['options'] : [];
347-
return ($attributeCode == 'country_id') ? $this->orderCountryOptions($options) : $options;
356+
return isset($attributeConfig['options']) ? $attributeConfig['options'] : [];
348357
}
349358

350359
/**
351360
* Order country options. Move top countries to the beginning of the list.
352361
*
353362
* @param array $countryOptions
354363
* @return array
364+
* @deprecated
355365
*/
356366
protected function orderCountryOptions(array $countryOptions)
357367
{
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Checkout\Block\Checkout;
7+
8+
use Magento\Directory\Helper\Data as DirectoryHelper;
9+
use Magento\Store\Api\StoreResolverInterface;
10+
11+
/**
12+
* Directory data processor.
13+
*
14+
* This class adds various country and region dictionaries to checkout page.
15+
* This data can be used by other UI components during checkout flow.
16+
*/
17+
class DirectoryDataProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $countryOptions;
23+
24+
/**
25+
* @var array
26+
*/
27+
private $regionOptions;
28+
29+
/**
30+
* @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
31+
*/
32+
private $regionCollectionFactory;
33+
34+
/**
35+
* @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
36+
*/
37+
private $countryCollectionFactory;
38+
39+
/**
40+
* @var StoreResolverInterface
41+
*/
42+
private $storeResolver;
43+
44+
/**
45+
* @var DirectoryHelper
46+
*/
47+
private $directoryHelper;
48+
49+
/**
50+
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection
51+
* @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection
52+
* @param StoreResolverInterface $storeResolver
53+
* @param DirectoryHelper $directoryHelper
54+
*/
55+
public function __construct(
56+
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection,
57+
\Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection,
58+
StoreResolverInterface $storeResolver,
59+
DirectoryHelper $directoryHelper
60+
) {
61+
$this->countryCollectionFactory = $countryCollection;
62+
$this->regionCollectionFactory = $regionCollection;
63+
$this->storeResolver = $storeResolver;
64+
$this->directoryHelper = $directoryHelper;
65+
}
66+
67+
/**
68+
* Process js Layout of block
69+
*
70+
* @param array $jsLayout
71+
* @return array
72+
*/
73+
public function process($jsLayout)
74+
{
75+
if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
76+
$jsLayout['components']['checkoutProvider']['dictionaries'] = [
77+
'country_id' => $this->getCountryOptions(),
78+
'region_id' => $this->getRegionOptions(),
79+
];
80+
}
81+
82+
return $jsLayout;
83+
}
84+
85+
/**
86+
* Get country options list.
87+
*
88+
* @return array
89+
*/
90+
private function getCountryOptions()
91+
{
92+
if (!isset($this->countryOptions)) {
93+
$this->countryOptions = $this->countryCollectionFactory->create()->loadByStore(
94+
$this->storeResolver->getCurrentStoreId()
95+
)->toOptionArray();
96+
$this->countryOptions = $this->orderCountryOptions($this->countryOptions);
97+
}
98+
99+
return $this->countryOptions;
100+
}
101+
102+
/**
103+
* Get region options list.
104+
*
105+
* @return array
106+
*/
107+
private function getRegionOptions()
108+
{
109+
if (!isset($this->regionOptions)) {
110+
$this->regionOptions = $this->regionCollectionFactory->create()->addAllowedCountriesFilter(
111+
$this->storeResolver->getCurrentStoreId()
112+
)->toOptionArray();
113+
}
114+
115+
return $this->regionOptions;
116+
}
117+
118+
/**
119+
* Sort country options by top country codes.
120+
*
121+
* @param array $countryOptions
122+
* @return array
123+
*/
124+
private function orderCountryOptions(array $countryOptions)
125+
{
126+
$topCountryCodes = $this->directoryHelper->getTopCountryCodes();
127+
if (empty($topCountryCodes)) {
128+
return $countryOptions;
129+
}
130+
131+
$headOptions = [];
132+
$tailOptions = [[
133+
'value' => 'delimiter',
134+
'label' => '──────────',
135+
'disabled' => true,
136+
]];
137+
foreach ($countryOptions as $countryOption) {
138+
if (empty($countryOption['value']) || in_array($countryOption['value'], $topCountryCodes)) {
139+
array_push($headOptions, $countryOption);
140+
} else {
141+
array_push($tailOptions, $countryOption);
142+
}
143+
}
144+
return array_merge($headOptions, $tailOptions);
145+
}
146+
}

app/code/Magento/Checkout/Block/Checkout/LayoutProcessor.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
use Magento\Checkout\Helper\Data;
99
use Magento\Framework\App\ObjectManager;
10+
use Magento\Store\Api\StoreResolverInterface;
1011

12+
/**
13+
* Class LayoutProcessor
14+
*/
1115
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
1216
{
1317
/**
@@ -35,6 +39,16 @@ class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcesso
3539
*/
3640
private $checkoutDataHelper;
3741

42+
/**
43+
* @var StoreResolverInterface
44+
*/
45+
private $storeResolver;
46+
47+
/**
48+
* @var \Magento\Shipping\Model\Config
49+
*/
50+
private $shippingConfig;
51+
3852
/**
3953
* @param \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider
4054
* @param \Magento\Ui\Component\Form\AttributeMapper $attributeMapper
@@ -146,6 +160,16 @@ public function process($jsLayout)
146160
$elements
147161
);
148162
}
163+
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
164+
['step-config']['children']['shipping-rates-validation']['children']
165+
)) {
166+
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
167+
['step-config']['children']['shipping-rates-validation']['children'] =
168+
$this->processShippingChildrenComponents(
169+
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
170+
['step-config']['children']['shipping-rates-validation']['children']
171+
);
172+
}
149173

150174
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
151175
['children']['shippingAddress']['children']['shipping-address-fieldset']['children']
@@ -163,6 +187,26 @@ public function process($jsLayout)
163187
return $jsLayout;
164188
}
165189

190+
/**
191+
* Process shipping configuration to exclude inactive carriers.
192+
*
193+
* @param array $shippingRatesLayout
194+
* @return array
195+
*/
196+
private function processShippingChildrenComponents($shippingRatesLayout)
197+
{
198+
$activeCarriers = $this->getShippingConfig()->getActiveCarriers(
199+
$this->getStoreResolver()->getCurrentStoreId()
200+
);
201+
foreach (array_keys($shippingRatesLayout) as $carrierName) {
202+
$carrierKey = str_replace('-rates-validation', '', $carrierName);
203+
if (!array_key_exists($carrierKey, $activeCarriers)) {
204+
unset($shippingRatesLayout[$carrierName]);
205+
}
206+
}
207+
return $shippingRatesLayout;
208+
}
209+
166210
/**
167211
* Appends billing address form component to payment layout
168212
*
@@ -326,4 +370,34 @@ private function getCheckoutDataHelper()
326370

327371
return $this->checkoutDataHelper;
328372
}
373+
374+
/**
375+
* Get active carriers list.
376+
*
377+
* @return array
378+
* @deprecated
379+
*/
380+
private function getShippingConfig()
381+
{
382+
if (!$this->shippingConfig) {
383+
$this->shippingConfig = ObjectManager::getInstance()->get(\Magento\Shipping\Model\Config::class);
384+
}
385+
386+
return $this->shippingConfig;
387+
}
388+
389+
/**
390+
* Get store resolver.
391+
*
392+
* @return StoreResolverInterface
393+
* @deprecated
394+
*/
395+
private function getStoreResolver()
396+
{
397+
if (!$this->storeResolver) {
398+
$this->storeResolver = ObjectManager::getInstance()->get(StoreResolverInterface::class);
399+
}
400+
401+
return $this->storeResolver;
402+
}
329403
}

app/code/Magento/Checkout/Test/Unit/Block/Cart/LayoutProcessorTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,20 @@ public function testProcess()
6969
$this->countryCollection->expects($this->once())->method('loadByStore')->willReturnSelf();
7070
$this->countryCollection->expects($this->once())->method('toOptionArray')->willReturn($countries);
7171

72-
$this->regionCollection->expects($this->once())->method('load')->willReturnSelf();
72+
$this->regionCollection->expects($this->once())->method('addAllowedCountriesFilter')->willReturnSelf();
7373
$this->regionCollection->expects($this->once())->method('toOptionArray')->willReturn($regions);
7474

7575
$layoutMerged = $layout;
7676
$layoutMerged['components']['block-summary']['children']['block-shipping']['children']
7777
['address-fieldsets']['children']['fieldThree'] = ['param' => 'value'];
7878
$layoutMergedPointer = &$layoutMerged['components']['block-summary']['children']['block-shipping']
7979
['children']['address-fieldsets']['children'];
80-
80+
$layoutMerged['components']['checkoutProvider'] = [
81+
'dictionaries' => [
82+
'country_id' => [],
83+
'region_id' => [],
84+
]
85+
];
8186
$elements = [
8287
'city' => [
8388
'visible' => false,

0 commit comments

Comments
 (0)