Skip to content

Commit eb42dea

Browse files
committed
MAGETWO-92884: Shipping page contains the equal shipping addresses after adding a user.
1 parent e47b0d7 commit eb42dea

File tree

3 files changed

+153
-27
lines changed

3 files changed

+153
-27
lines changed

app/code/Magento/Sales/Model/Order/OrderCustomerExtractor.php

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
use Magento\Customer\Api\Data\AddressInterface;
1515
use Magento\Customer\Api\Data\RegionInterface;
1616
use Magento\Customer\Api\Data\AddressInterfaceFactory as AddressFactory;
17-
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1817
use Magento\Customer\Api\Data\RegionInterfaceFactory as RegionFactory;
1918
use Magento\Customer\Api\Data\CustomerInterfaceFactory as CustomerFactory;
2019
use Magento\Quote\Api\Data\AddressInterfaceFactory as QuoteAddressFactory;
20+
use Magento\Sales\Model\Order\Address as OrderAddress;
2121

2222
/**
2323
* Extract customer data from an order.
@@ -87,8 +87,9 @@ public function __construct(
8787
}
8888

8989
/**
90-
* @param int $orderId
90+
* Extract customer data from order.
9191
*
92+
* @param int $orderId
9293
* @return CustomerInterface
9394
*/
9495
public function extract(int $orderId): CustomerInterface
@@ -107,36 +108,45 @@ public function extract(int $orderId): CustomerInterface
107108
$order->getBillingAddress(),
108109
[]
109110
);
110-
$addresses = $order->getAddresses();
111-
foreach ($addresses as $address) {
112-
$addressData = $this->objectCopyService->copyFieldsetToTarget(
113-
'order_address',
114-
'to_customer_address',
115-
$address,
116-
[]
117-
);
118-
/** @var AddressInterface $customerAddress */
119-
$customerAddress = $this->addressFactory->create(['data' => $addressData]);
120-
switch ($address->getAddressType()) {
121-
case QuoteAddress::ADDRESS_TYPE_BILLING:
122-
$customerAddress->setIsDefaultBilling(true);
123-
break;
124-
case QuoteAddress::ADDRESS_TYPE_SHIPPING:
125-
$customerAddress->setIsDefaultShipping(true);
126-
break;
111+
112+
$processedAddressData = [];
113+
$customerAddresses = [];
114+
foreach ($order->getAddresses() as $orderAddress) {
115+
$addressData = $this->objectCopyService
116+
->copyFieldsetToTarget('order_address', 'to_customer_address', $orderAddress, []);
117+
118+
$index = array_search($addressData, $processedAddressData);
119+
if ($index === false) {
120+
// create new customer address only if it is unique
121+
$customerAddress = $this->addressFactory->create(['data' => $addressData]);
122+
$customerAddress->setIsDefaultBilling(false);
123+
$customerAddress->setIsDefaultBilling(false);
124+
if (is_string($orderAddress->getRegion())) {
125+
/** @var RegionInterface $region */
126+
$region = $this->regionFactory->create();
127+
$region->setRegion($orderAddress->getRegion());
128+
$region->setRegionCode($orderAddress->getRegionCode());
129+
$region->setRegionId($orderAddress->getRegionId());
130+
$customerAddress->setRegion($region);
131+
}
132+
133+
$processedAddressData[] = $addressData;
134+
$customerAddresses[] = $customerAddress;
135+
$index = count($processedAddressData) - 1;
127136
}
128137

129-
if (is_string($address->getRegion())) {
130-
/** @var RegionInterface $region */
131-
$region = $this->regionFactory->create();
132-
$region->setRegion($address->getRegion());
133-
$region->setRegionCode($address->getRegionCode());
134-
$region->setRegionId($address->getRegionId());
135-
$customerAddress->setRegion($region);
138+
$customerAddress = $customerAddresses[$index];
139+
// make sure that address type flags from equal addresses are stored in one resulted address
140+
if ($orderAddress->getAddressType() == OrderAddress::TYPE_BILLING) {
141+
$customerAddress->setIsDefaultBilling(true);
142+
}
143+
if ($orderAddress->getAddressType() == OrderAddress::TYPE_SHIPPING) {
144+
$customerAddress->setIsDefaultShipping(true);
136145
}
137-
$customerData['addresses'][] = $customerAddress;
138146
}
139147

148+
$customerData['addresses'] = $customerAddresses;
149+
140150
return $this->customerFactory->create(['data' => $customerData]);
141151
}
142152
}

dev/tests/integration/testsuite/Magento/Sales/Api/OrderCustomerDelegateInterfaceTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,60 @@ public function testDelegateNew(): void
159159
'12345abcD'
160160
);
161161

162+
//Testing that addresses from order and the order itself are assigned
163+
//to customer.
164+
$order = $this->orderRepository->get($orderId);
165+
$this->assertCount(1, $createdCustomer->getAddresses());
166+
$this->assertNotNull($createdCustomer->getDefaultBilling());
167+
$this->assertNotNull($createdCustomer->getDefaultShipping());
168+
foreach ($createdCustomer->getAddresses() as $address) {
169+
$this->assertTrue(
170+
$address->isDefaultBilling() || $address->isDefaultShipping()
171+
);
172+
if ($address->isDefaultBilling()) {
173+
$this->compareAddresses($order->getBillingAddress(), $address);
174+
} elseif ($address->isDefaultShipping()) {
175+
$this->compareAddresses($order->getShippingAddress(), $address);
176+
}
177+
}
178+
$this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
179+
}
180+
181+
/**
182+
* @magentoDbIsolation enabled
183+
* @magentoAppIsolation enabled
184+
* @magentoDataFixture Magento/Sales/_files/order_different_addresses.php
185+
*/
186+
public function testDelegateNewDifferentAddresses()
187+
{
188+
$orderAutoincrementId = '100000001';
189+
/** @var Order $orderModel */
190+
$orderModel = $this->orderFactory->create();
191+
$orderModel->loadByIncrementId($orderAutoincrementId);
192+
$orderId = $orderModel->getId();
193+
unset($orderModel);
194+
195+
$this->delegate->delegateNew($orderId);
196+
197+
//Saving new customer with prepared data from order.
198+
/** @var CustomerInterface $customer */
199+
$customer = $this->customerFactory->create();
200+
$customer->setWebsiteId(1)
201+
->setEmail('customer_order_delegate@example.com')
202+
->setGroupId(1)
203+
->setStoreId(1)
204+
->setPrefix('Mr.')
205+
->setFirstname('John')
206+
->setMiddlename('A')
207+
->setLastname('Smith')
208+
->setSuffix('Esq.')
209+
->setTaxvat('12')
210+
->setGender(0);
211+
$createdCustomer = $this->accountManagement->createAccount(
212+
$customer,
213+
'12345abcD'
214+
);
215+
162216
//Testing that addresses from order and the order itself are assigned
163217
//to customer.
164218
$order = $this->orderRepository->get($orderId);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
// @codingStandardsIgnoreFile
8+
9+
require 'default_rollback.php';
10+
require __DIR__ . '/../../../Magento/Catalog/_files/product_simple.php';
11+
/** @var \Magento\Catalog\Model\Product $product */
12+
13+
$addressData = include __DIR__ . '/address_data.php';
14+
15+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
16+
17+
/** @var \Magento\Sales\Model\Order\Address $billingAddress */
18+
$billingAddress = $objectManager->create(\Magento\Sales\Model\Order\Address::class, ['data' => $addressData]);
19+
$billingAddress->setAddressType('billing');
20+
21+
/** @var \Magento\Sales\Model\Order\Address $shippingAddress */
22+
$shippingAddress = clone $billingAddress;
23+
$shippingAddress
24+
->setId(null)
25+
->setAddressType('shipping')
26+
->setCity('San Francisco');
27+
28+
/** @var \Magento\Sales\Model\Order\Payment $payment */
29+
$payment = $objectManager->create(\Magento\Sales\Model\Order\Payment::class);
30+
$payment->setMethod('checkmo');
31+
$payment->setAdditionalInformation('last_trans_id', '11122');
32+
$payment->setAdditionalInformation('metadata', [
33+
'type' => 'free',
34+
'fraudulent' => false,
35+
]);
36+
37+
/** @var \Magento\Sales\Model\Order\Item $orderItem */
38+
$orderItem = $objectManager->create(\Magento\Sales\Model\Order\Item::class);
39+
$orderItem->setProductId($product->getId())->setQtyOrdered(2);
40+
$orderItem->setBasePrice($product->getPrice());
41+
$orderItem->setPrice($product->getPrice());
42+
$orderItem->setRowTotal($product->getPrice());
43+
$orderItem->setProductType('simple');
44+
45+
/** @var \Magento\Sales\Model\Order $order */
46+
$order = $objectManager->create(\Magento\Sales\Model\Order::class);
47+
$order->setIncrementId('100000001')
48+
->setState(\Magento\Sales\Model\Order::STATE_PROCESSING)
49+
->setStatus($order->getConfig()->getStateDefaultStatus(\Magento\Sales\Model\Order::STATE_PROCESSING))
50+
->setSubtotal(100)
51+
->setGrandTotal(100)
52+
->setBaseSubtotal(100)
53+
->setBaseGrandTotal(100)
54+
->setCustomerIsGuest(true)
55+
->setCustomerEmail('customer@null.com')
56+
->setBillingAddress($billingAddress)
57+
->setShippingAddress($shippingAddress)
58+
->setStoreId($objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->getStore()->getId())
59+
->addItem($orderItem)
60+
->setPayment($payment);
61+
62+
$order->save();

0 commit comments

Comments
 (0)