Skip to content

Commit

Permalink
MAGETWO-59685: Checkout pages very slow due to pulling in JSON config…
Browse files Browse the repository at this point in the history
…, js scripts for unused payment & shipping methods #6997 #4868
  • Loading branch information
omiroshnichenko committed Nov 2, 2016
1 parent 8bde202 commit 6a5ece2
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 38 deletions.
11 changes: 9 additions & 2 deletions app/code/Magento/Checkout/Block/Cart/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public function process($jsLayout)
'visible' => true,
'formElement' => 'select',
'label' => __('Country'),
'options' => $this->countryCollection->loadByStore()->toOptionArray(),
'options' => [],
'value' => null
],
'region_id' => [
'visible' => true,
'formElement' => 'select',
'label' => __('State/Province'),
'options' => $this->regionCollection->load()->toOptionArray(),
'options' => [],
'value' => null
],
'postcode' => [
Expand All @@ -103,6 +103,13 @@ public function process($jsLayout)
]
];

if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
$jsLayout['components']['checkoutProvider']['dictionaries'] = [
'country_id' => $this->countryCollection->loadByStore()->toOptionArray(),
'region_id' => $this->regionCollection->addAllowedCountriesFilter()->toOptionArray(),
];
}

if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
['address-fieldsets']['children'])
) {
Expand Down
14 changes: 12 additions & 2 deletions app/code/Magento/Checkout/Block/Checkout/AttributeMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ protected function getFieldConfig(
'visible' => isset($additionalConfig['visible']) ? $additionalConfig['visible'] : true,
];

if ($attributeCode === 'region_id' || $attributeCode === 'country_id') {
unset($element['options']);
$element['deps'] = [$providerName];
$element['imports'] = [
'initialOptions' => 'index = ' . $providerName . ':dictionaries.' . $attributeCode,
'setOptions' => 'index = ' . $providerName . ':dictionaries.' . $attributeCode
];
}

if (isset($attributeConfig['value']) && $attributeConfig['value'] != null) {
$element['value'] = $attributeConfig['value'];
} elseif (isset($attributeConfig['default']) && $attributeConfig['default'] != null) {
Expand Down Expand Up @@ -341,18 +350,19 @@ protected function getCustomer()
* @param string $attributeCode
* @param array $attributeConfig
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function getFieldOptions($attributeCode, array $attributeConfig)
{
$options = isset($attributeConfig['options']) ? $attributeConfig['options'] : [];
return ($attributeCode == 'country_id') ? $this->orderCountryOptions($options) : $options;
return isset($attributeConfig['options']) ? $attributeConfig['options'] : [];
}

/**
* Order country options. Move top countries to the beginning of the list.
*
* @param array $countryOptions
* @return array
* @deprecated
*/
protected function orderCountryOptions(array $countryOptions)
{
Expand Down
146 changes: 146 additions & 0 deletions app/code/Magento/Checkout/Block/Checkout/DirectoryDataProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Checkout\Block\Checkout;

use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Store\Api\StoreResolverInterface;

/**
* Directory data processor.
*
* This class adds various country and region dictionaries to checkout page.
* This data can be used by other UI components during checkout flow.
*/
class DirectoryDataProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
{
/**
* @var array
*/
private $countryOptions;

/**
* @var array
*/
private $regionOptions;

/**
* @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
*/
private $regionCollectionFactory;

/**
* @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
*/
private $countryCollectionFactory;

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

/**
* @var DirectoryHelper
*/
private $directoryHelper;

/**
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection
* @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection
* @param StoreResolverInterface $storeResolver
* @param DirectoryHelper $directoryHelper
*/
public function __construct(
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection,
\Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection,
StoreResolverInterface $storeResolver,
DirectoryHelper $directoryHelper
) {
$this->countryCollectionFactory = $countryCollection;
$this->regionCollectionFactory = $regionCollection;
$this->storeResolver = $storeResolver;
$this->directoryHelper = $directoryHelper;
}

/**
* Process js Layout of block
*
* @param array $jsLayout
* @return array
*/
public function process($jsLayout)
{
if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
$jsLayout['components']['checkoutProvider']['dictionaries'] = [
'country_id' => $this->getCountryOptions(),
'region_id' => $this->getRegionOptions(),
];
}

return $jsLayout;
}

/**
* Get country options list.
*
* @return array
*/
private function getCountryOptions()
{
if (!isset($this->countryOptions)) {
$this->countryOptions = $this->countryCollectionFactory->create()->loadByStore(
$this->storeResolver->getCurrentStoreId()
)->toOptionArray();
$this->countryOptions = $this->orderCountryOptions($this->countryOptions);
}

return $this->countryOptions;
}

/**
* Get region options list.
*
* @return array
*/
private function getRegionOptions()
{
if (!isset($this->regionOptions)) {
$this->regionOptions = $this->regionCollectionFactory->create()->addAllowedCountriesFilter(
$this->storeResolver->getCurrentStoreId()
)->toOptionArray();
}

return $this->regionOptions;
}

/**
* Sort country options by top country codes.
*
* @param array $countryOptions
* @return array
*/
private function orderCountryOptions(array $countryOptions)
{
$topCountryCodes = $this->directoryHelper->getTopCountryCodes();
if (empty($topCountryCodes)) {
return $countryOptions;
}

$headOptions = [];
$tailOptions = [[
'value' => 'delimiter',
'label' => '──────────',
'disabled' => true,
]];
foreach ($countryOptions as $countryOption) {
if (empty($countryOption['value']) || in_array($countryOption['value'], $topCountryCodes)) {
array_push($headOptions, $countryOption);
} else {
array_push($tailOptions, $countryOption);
}
}
return array_merge($headOptions, $tailOptions);
}
}
74 changes: 74 additions & 0 deletions app/code/Magento/Checkout/Block/Checkout/LayoutProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

use Magento\Checkout\Helper\Data;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Api\StoreResolverInterface;

/**
* Class LayoutProcessor
*/
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
{
/**
Expand Down Expand Up @@ -35,6 +39,16 @@ class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcesso
*/
private $checkoutDataHelper;

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

/**
* @var \Magento\Shipping\Model\Config
*/
private $shippingConfig;

/**
* @param \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider
* @param \Magento\Ui\Component\Form\AttributeMapper $attributeMapper
Expand Down Expand Up @@ -146,6 +160,16 @@ public function process($jsLayout)
$elements
);
}
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['step-config']['children']['shipping-rates-validation']['children']
)) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['step-config']['children']['shipping-rates-validation']['children'] =
$this->processShippingChildrenComponents(
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['step-config']['children']['shipping-rates-validation']['children']
);
}

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

/**
* Process shipping configuration to exclude inactive carriers.
*
* @param array $shippingRatesLayout
* @return array
*/
private function processShippingChildrenComponents($shippingRatesLayout)
{
$activeCarriers = $this->getShippingConfig()->getActiveCarriers(
$this->getStoreResolver()->getCurrentStoreId()
);
foreach (array_keys($shippingRatesLayout) as $carrierName) {
$carrierKey = str_replace('-rates-validation', '', $carrierName);
if (!array_key_exists($carrierKey, $activeCarriers)) {
unset($shippingRatesLayout[$carrierName]);
}
}
return $shippingRatesLayout;
}

/**
* Appends billing address form component to payment layout
* @param array $paymentLayout
Expand Down Expand Up @@ -314,4 +358,34 @@ private function getCheckoutDataHelper()

return $this->checkoutDataHelper;
}

/**
* Get active carriers list.
*
* @return array
* @deprecated
*/
private function getShippingConfig()
{
if (!$this->shippingConfig) {
$this->shippingConfig = ObjectManager::getInstance()->get(\Magento\Shipping\Model\Config::class);
}

return $this->shippingConfig;
}

/**
* Get store resolver.
*
* @return StoreResolverInterface
* @deprecated
*/
private function getStoreResolver()
{
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 @@ -69,15 +69,20 @@ public function testProcess()
$this->countryCollection->expects($this->once())->method('loadByStore')->willReturnSelf();
$this->countryCollection->expects($this->once())->method('toOptionArray')->willReturn($countries);

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

$layoutMerged = $layout;
$layoutMerged['components']['block-summary']['children']['block-shipping']['children']
['address-fieldsets']['children']['fieldThree'] = ['param' => 'value'];
$layoutMergedPointer = &$layoutMerged['components']['block-summary']['children']['block-shipping']
['children']['address-fieldsets']['children'];

$layoutMerged['components']['checkoutProvider'] = [
'dictionaries' => [
'country_id' => [],
'region_id' => [],
]
];
$elements = [
'city' => [
'visible' => false,
Expand Down
Loading

0 comments on commit 6a5ece2

Please sign in to comment.