Skip to content

Commit

Permalink
Merge pull request #3538 from magento-performance/MAGETWO-97151
Browse files Browse the repository at this point in the history
Fixed issues:
  - MAGETWO-97151 [Backport][MC-5683] Implement handling of large number of addresses on admin order creation page
  • Loading branch information
igrybkov authored Dec 17, 2018
2 parents 75c9b64 + b819f2b commit a0614c2
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 12 deletions.
131 changes: 131 additions & 0 deletions app/code/Magento/Sales/ViewModel/Customer/AddressFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\ViewModel\Customer;

use Magento\Framework\View\Element\Block\ArgumentInterface;

/**
* Customer address formatter
*/
class AddressFormatter implements ArgumentInterface
{
/**
* Customer form factory
*
* @var \Magento\Customer\Model\Metadata\FormFactory
*/
private $customerFormFactory;

/**
* Address format helper
*
* @var \Magento\Customer\Helper\Address
*/
private $addressFormatHelper;

/**
* Directory helper
*
* @var \Magento\Directory\Helper\Data
*/
private $directoryHelper;

/**
* Session quote
*
* @var \Magento\Backend\Model\Session\Quote
*/
private $session;

/**
* Json encoder
*
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $jsonEncoder;

/**
* Customer address
*
* @param \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory
* @param \Magento\Customer\Helper\Address $addressFormatHelper
* @param \Magento\Directory\Helper\Data $directoryHelper
* @param \Magento\Backend\Model\Session\Quote $session
* @param \Magento\Framework\Serialize\Serializer\Json $jsonEncoder
*/
public function __construct(
\Magento\Customer\Model\Metadata\FormFactory $customerFormFactory,
\Magento\Customer\Helper\Address $addressFormatHelper,
\Magento\Directory\Helper\Data $directoryHelper,
\Magento\Backend\Model\Session\Quote $session,
\Magento\Framework\Serialize\Serializer\Json $jsonEncoder
) {
$this->customerFormFactory = $customerFormFactory;
$this->addressFormatHelper = $addressFormatHelper;
$this->directoryHelper = $directoryHelper;
$this->session = $session;
$this->jsonEncoder = $jsonEncoder;
}

/**
* Return customer address array as JSON
*
* @param array $addressArray
*
* @return string
*/
public function getAddressesJson(array $addressArray)
{
$data = $this->getEmptyAddressForm();
foreach ($addressArray as $addressId => $address) {
$addressForm = $this->customerFormFactory->create(
'customer_address',
'adminhtml_customer_address',
$address
);
$data[$addressId] = $addressForm->outputData(
\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON
);
}

return $this->jsonEncoder->serialize($data);
}

/**
* Represent customer address in 'online' format.
*
* @param array $address
* @return string
*/
public function getAddressAsString(array $address)
{
$formatTypeRenderer = $this->addressFormatHelper->getFormatTypeRenderer('oneline');
$result = '';
if ($formatTypeRenderer) {
$result = $formatTypeRenderer->renderArray($address);
}

return $result;
}

/**
* Return empty address address form
*
* @return array
*/
private function getEmptyAddressForm()
{
$defaultCountryId = $this->directoryHelper->getDefaultCountry($this->session->getStore());
$emptyAddressForm = $this->customerFormFactory->create(
'customer_address',
'adminhtml_customer_address',
[\Magento\Customer\Api\Data\AddressInterface::COUNTRY_ID => $defaultCountryId]
);

return [0 => $emptyAddressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\Pviewed" template="Magento_Sales::order/create/sidebar/items.phtml" name="pviewed"/>
</block>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Form\Account" template="Magento_Sales::order/create/form/account.phtml" name="form_account"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
<arguments>
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
</arguments>
</block>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
<arguments>
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
</arguments>
</block>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method" template="Magento_Sales::order/create/abstract.phtml" name="shipping_method">
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\Form" template="Magento_Sales::order/create/shipping/method/form.phtml" name="order_create_shipping_form" as="form"/>
</block>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
<arguments>
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\Pviewed" template="Magento_Sales::order/create/sidebar/items.phtml" name="pviewed"/>
</block>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Form\Account" template="Magento_Sales::order/create/form/account.phtml" name="form_account"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
<arguments>
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
</arguments>
</block>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
<arguments>
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
</arguments>
</block>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method" template="Magento_Sales::order/create/abstract.phtml" name="shipping_method">
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method\Form" template="Magento_Sales::order/create/shipping/method/form.phtml" name="order.create.shipping.method.form" as="form"/>
</block>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address"/>
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
<arguments>
<argument name="customerAddressFormatter" xsi:type="object">Magento\Sales\ViewModel\Customer\AddressFormatter</argument>
<argument name="customerAddressCollection" xsi:type="object">Magento\Customer\Model\ResourceModel\Address\Collection</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

// @codingStandardsIgnoreFile

/**
* @var \Magento\Customer\Model\ResourceModel\Address\Collection $addressCollection
*/
$addressCollection = $block->getData('customerAddressCollection');

$addressArray = [];
if ($block->getCustomerId()) {
$addressArray = $addressCollection->setCustomerFilter([$block->getCustomerId()])->toArray();
}

/**
* @var \Magento\Sales\ViewModel\Customer\AddressFormatter $customerAddressFormatter
*/
$customerAddressFormatter = $block->getData('customerAddressFormatter');

/**
* @var \Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address|\Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address $block
*/
Expand All @@ -17,7 +32,7 @@ if ($block->getIsShipping()):
require(["Magento_Sales/order/create/form"], function(){

order.shippingAddressContainer = '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>';
order.setAddresses(<?= /* @escapeNotVerified */ $block->getAddressCollectionJson() ?>);
order.setAddresses(<?= /* @escapeNotVerified */ $customerAddressFormatter->getAddressesJson($addressArray) ?>);

});
</script>
Expand Down Expand Up @@ -59,13 +74,11 @@ endif; ?>
onchange="order.selectAddress(this, '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>')"
class="admin__control-select">
<option value=""><?= /* @escapeNotVerified */ __('Add New Address') ?></option>
<?php foreach ($block->getAddressCollection() as $_address): ?>
<?php //if($block->getAddressAsString($_address)!=$block->getAddressAsString($block->getAddress())): ?>
<?php foreach ($addressArray as $addressId => $address): ?>
<option
value="<?= /* @escapeNotVerified */ $_address->getId() ?>"<?php if ($_address->getId() == $block->getAddressId()): ?> selected="selected"<?php endif; ?>>
<?= /* @escapeNotVerified */ $block->getAddressAsString($_address) ?>
value="<?= /* @escapeNotVerified */ $addressId ?>"<?php if ($addressId == $block->getAddressId()): ?> selected="selected"<?php endif; ?>>
<?= /* @escapeNotVerified */ $block->escapeHtml($customerAddressFormatter->getAddressAsString($address)) ?>
</option>
<?php //endif; ?>
<?php endforeach; ?>
</select>
</div>
Expand Down
33 changes: 33 additions & 0 deletions setup/performance-toolkit/benchmark.jmx
Original file line number Diff line number Diff line change
Expand Up @@ -25803,6 +25803,39 @@ catch (java.lang.Exception e) {
</ResponseAssertion>
<hashTree/>
</hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="Filled Order Page" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="HTTPSampler.domain"/>
<stringProp name="HTTPSampler.port"/>
<stringProp name="HTTPSampler.connect_timeout">60000</stringProp>
<stringProp name="HTTPSampler.response_timeout">200000</stringProp>
<stringProp name="HTTPSampler.protocol">${request_protocol}</stringProp>
<stringProp name="HTTPSampler.contentEncoding"/>
<stringProp name="HTTPSampler.path">${base_path}${admin_path}/sales/order_create/index/</stringProp>
<stringProp name="HTTPSampler.method">GET</stringProp>
<boolProp name="HTTPSampler.follow_redirects">true</boolProp>
<boolProp name="HTTPSampler.auto_redirects">false</boolProp>
<boolProp name="HTTPSampler.use_keepalive">true</boolProp>
<boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
<boolProp name="HTTPSampler.monitor">false</boolProp>
<stringProp name="HTTPSampler.embedded_url_re"/>
<stringProp name="TestPlan.comments">Detected the start of a redirect chain</stringProp>
</HTTPSamplerProxy>
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Assert Filled Order Page" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="-37823069">Select from existing customer addresses</stringProp>
<stringProp name="-13185722">Submit Order</stringProp>
<stringProp name="-209419315">Items Ordered</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
<intProp name="Assertion.test_type">2</intProp>
</ResponseAssertion>
<hashTree/>
</hashTree>
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="Save Order" enabled="true">
<elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" enabled="true">
<collectionProp name="Arguments.arguments">
Expand Down

0 comments on commit a0614c2

Please sign in to comment.