-
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-59685: Checkout pages very slow due to pulling in JSON config…
- Loading branch information
1 parent
8bde202
commit 6a5ece2
Showing
12 changed files
with
470 additions
and
38 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
146 changes: 146 additions & 0 deletions
146
app/code/Magento/Checkout/Block/Checkout/DirectoryDataProcessor.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,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); | ||
} | ||
} |
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
Oops, something went wrong.