Skip to content

Commit c6af508

Browse files
committed
Merge branch 'MC-5972' of github.com:magento-performance/magento2ce into 2.3-develop
2 parents 5a298ad + 2becc2f commit c6af508

File tree

6 files changed

+209
-12
lines changed

6 files changed

+209
-12
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model\Customer;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\View\Element\Block\ArgumentInterface;
12+
13+
/**
14+
* Customer address
15+
*/
16+
class Address extends DataObject implements ArgumentInterface
17+
{
18+
/**
19+
* Customer addresses collection
20+
*
21+
* @var \Magento\Customer\Model\ResourceModel\Address\Collection
22+
*/
23+
private $addressCollection;
24+
25+
/**
26+
* Customer address array
27+
*
28+
* @var array
29+
*/
30+
private $addressArray;
31+
32+
/**
33+
* Customer form factory
34+
*
35+
* @var \Magento\Customer\Model\Metadata\FormFactory
36+
*/
37+
private $customerFormFactory;
38+
39+
/**
40+
* Address helper
41+
*
42+
* @var \Magento\Customer\Helper\Address
43+
*/
44+
private $addressHelper;
45+
46+
/**
47+
* Directory helper
48+
*
49+
* @var \Magento\Directory\Helper\Data
50+
*/
51+
private $directoryHelper;
52+
53+
/**
54+
* Session quote
55+
*
56+
* @var \Magento\Backend\Model\Session\Quote
57+
*/
58+
private $session;
59+
60+
/**
61+
* Json encoder
62+
*
63+
* @var \Magento\Framework\Serialize\Serializer\Json
64+
*/
65+
private $jsonEncoder;
66+
67+
/**
68+
* Customer address
69+
*
70+
* @param \Magento\Customer\Model\ResourceModel\Address\Collection $addressCollection
71+
* @param \Magento\Customer\Model\Metadata\FormFactory $customerFormFactory
72+
* @param \Magento\Customer\Helper\Address $addressHelper
73+
* @param \Magento\Directory\Helper\Data $directoryHelper
74+
* @param \Magento\Backend\Model\Session\Quote $session
75+
* @param \Magento\Framework\Serialize\Serializer\Json $jsonEncoder
76+
*/
77+
public function __construct(
78+
\Magento\Customer\Model\ResourceModel\Address\Collection $addressCollection,
79+
\Magento\Customer\Model\Metadata\FormFactory $customerFormFactory,
80+
\Magento\Customer\Helper\Address $addressHelper,
81+
\Magento\Directory\Helper\Data $directoryHelper,
82+
\Magento\Backend\Model\Session\Quote $session,
83+
\Magento\Framework\Serialize\Serializer\Json $jsonEncoder
84+
) {
85+
$this->addressCollection = $addressCollection;
86+
$this->customerFormFactory = $customerFormFactory;
87+
$this->addressHelper = $addressHelper;
88+
$this->directoryHelper = $directoryHelper;
89+
$this->session = $session;
90+
$this->jsonEncoder = $jsonEncoder;
91+
parent::__construct();
92+
}
93+
94+
/**
95+
* Retrieve customer address array.
96+
*
97+
* @param int $customerId
98+
*
99+
* @return array
100+
*/
101+
public function getAddresses(int $customerId): array
102+
{
103+
if ($customerId) {
104+
if ($this->addressArray === null) {
105+
$addressCollection = $this->addressCollection->setCustomerFilter([$customerId]);
106+
$this->addressArray = $addressCollection->toArray();
107+
}
108+
return $this->addressArray;
109+
}
110+
return [];
111+
}
112+
113+
/**
114+
* Return customer address array as JSON
115+
*
116+
* @param int $customerId
117+
*
118+
* @return string
119+
*/
120+
public function getAddressesJson(int $customerId): string
121+
{
122+
$data = $this->getEmptyAddressForm();
123+
foreach ($this->getAddresses($customerId) as $addressId => $address) {
124+
$addressForm = $this->customerFormFactory->create(
125+
'customer_address',
126+
'adminhtml_customer_address',
127+
$address
128+
);
129+
$data[$addressId] = $addressForm->outputData(
130+
\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON
131+
);
132+
}
133+
134+
return $this->jsonEncoder->serialize($data);
135+
}
136+
137+
/**
138+
* Represent customer address in 'online' format.
139+
*
140+
* @param array $address
141+
* @return string
142+
*/
143+
public function getAddressAsString(array $address): string
144+
{
145+
$formatTypeRenderer = $this->addressHelper->getFormatTypeRenderer('oneline');
146+
$result = '';
147+
if ($formatTypeRenderer) {
148+
$result = $formatTypeRenderer->renderArray($address);
149+
}
150+
151+
return $result;
152+
}
153+
154+
/**
155+
* Return empty address address form
156+
*
157+
* @return array
158+
*/
159+
private function getEmptyAddressForm(): array
160+
{
161+
$defaultCountryId = $this->directoryHelper->getDefaultCountry($this->session->getStore());
162+
$emptyAddressForm = $this->customerFormFactory->create(
163+
'customer_address',
164+
'adminhtml_customer_address',
165+
[\Magento\Customer\Api\Data\AddressInterface::COUNTRY_ID => $defaultCountryId]
166+
);
167+
168+
return [0 => $emptyAddressForm->outputData(\Magento\Eav\Model\AttributeDataFactory::OUTPUT_FORMAT_JSON)];
169+
}
170+
}

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_index.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@
4545
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\Pviewed" template="Magento_Sales::order/create/sidebar/items.phtml" name="pviewed"/>
4646
</block>
4747
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Form\Account" template="Magento_Sales::order/create/form/account.phtml" name="form_account"/>
48-
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address"/>
49-
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address"/>
48+
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
49+
<arguments>
50+
<argument name="customerAddressModel" xsi:type="object">Magento\Sales\Model\Customer\Address</argument>
51+
</arguments>
52+
</block>
53+
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
54+
<arguments>
55+
<argument name="customerAddressModel" xsi:type="object">Magento\Sales\Model\Customer\Address</argument>
56+
</arguments>
57+
</block>
5058
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method" template="Magento_Sales::order/create/abstract.phtml" name="shipping_method">
5159
<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"/>
5260
</block>

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_billing_address.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<referenceContainer name="content">
11-
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address"/>
11+
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
12+
<arguments>
13+
<argument name="customerAddressModel" xsi:type="object">Magento\Sales\Model\Customer\Address</argument>
14+
</arguments>
15+
</block>
1216
</referenceContainer>
1317
</body>
1418
</page>

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_data.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@
2020
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\Pviewed" template="Magento_Sales::order/create/sidebar/items.phtml" name="pviewed"/>
2121
</block>
2222
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Form\Account" template="Magento_Sales::order/create/form/account.phtml" name="form_account"/>
23-
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address"/>
24-
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address"/>
23+
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
24+
<arguments>
25+
<argument name="customerAddressModel" xsi:type="object">Magento\Sales\Model\Customer\Address</argument>
26+
</arguments>
27+
</block>
28+
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address" template="Magento_Sales::order/create/form/address.phtml" name="billing_address">
29+
<arguments>
30+
<argument name="customerAddressModel" xsi:type="object">Magento\Sales\Model\Customer\Address</argument>
31+
</arguments>
32+
</block>
2533
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Method" template="Magento_Sales::order/create/abstract.phtml" name="shipping_method">
2634
<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"/>
2735
</block>

app/code/Magento/Sales/view/adminhtml/layout/sales_order_create_load_block_shipping_address.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
99
<body>
1010
<referenceContainer name="content">
11-
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address"/>
11+
<block class="Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address" template="Magento_Sales::order/create/form/address.phtml" name="shipping_address">
12+
<arguments>
13+
<argument name="customerAddressModel" xsi:type="object">Magento\Sales\Model\Customer\Address</argument>
14+
</arguments>
15+
</block>
1216
</referenceContainer>
1317
</body>
1418
</page>

app/code/Magento/Sales/view/adminhtml/templates/order/create/form/address.phtml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
// @codingStandardsIgnoreFile
88

9+
/**
10+
* @var Magento\Sales\Model\Customer\Address $customerAddressModel
11+
*/
12+
$customerAddressModel = $block->getData('customerAddressModel');
13+
914
/**
1015
* @var \Magento\Sales\Block\Adminhtml\Order\Create\Billing\Address|\Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address $block
1116
*/
@@ -17,7 +22,7 @@ if ($block->getIsShipping()):
1722
require(["Magento_Sales/order/create/form"], function(){
1823

1924
order.shippingAddressContainer = '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>';
20-
order.setAddresses(<?= /* @escapeNotVerified */ $block->getAddressCollectionJson() ?>);
25+
order.setAddresses(<?= /* @escapeNotVerified */ $customerAddressModel->getAddressesJson($block->getCustomerId()) ?>);
2126

2227
});
2328
</script>
@@ -59,13 +64,11 @@ endif; ?>
5964
onchange="order.selectAddress(this, '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>')"
6065
class="admin__control-select">
6166
<option value=""><?= /* @escapeNotVerified */ __('Add New Address') ?></option>
62-
<?php foreach ($block->getAddressCollection() as $_address): ?>
63-
<?php //if($block->getAddressAsString($_address)!=$block->getAddressAsString($block->getAddress())): ?>
67+
<?php foreach ($customerAddressModel->getAddresses($block->getCustomerId()) as $addressId => $address): ?>
6468
<option
65-
value="<?= /* @escapeNotVerified */ $_address->getId() ?>"<?php if ($_address->getId() == $block->getAddressId()): ?> selected="selected"<?php endif; ?>>
66-
<?= /* @escapeNotVerified */ $block->getAddressAsString($_address) ?>
69+
value="<?= /* @escapeNotVerified */ $addressId ?>"<?php if ($addressId == $block->getAddressId()): ?> selected="selected"<?php endif; ?>>
70+
<?= /* @escapeNotVerified */ $block->escapeHtml($customerAddressModel->getAddressAsString($address)) ?>
6771
</option>
68-
<?php //endif; ?>
6972
<?php endforeach; ?>
7073
</select>
7174
</div>

0 commit comments

Comments
 (0)