Skip to content

Commit 61d0e1c

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-92751' into 2.2-develop-pr32
2 parents d5a9ebe + d11b6ef commit 61d0e1c

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
@@ -15,10 +15,10 @@
1515
use Magento\Customer\Api\Data\AddressInterface;
1616
use Magento\Customer\Api\Data\RegionInterface;
1717
use Magento\Customer\Api\Data\AddressInterfaceFactory as AddressFactory;
18-
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1918
use Magento\Customer\Api\Data\RegionInterfaceFactory as RegionFactory;
2019
use Magento\Customer\Api\Data\CustomerInterfaceFactory as CustomerFactory;
2120
use Magento\Quote\Api\Data\AddressInterfaceFactory as QuoteAddressFactory;
21+
use Magento\Sales\Model\Order\Address as OrderAddress;
2222

2323
/**
2424
* Extract customer data from an order.
@@ -88,8 +88,9 @@ public function __construct(
8888
}
8989

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

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

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

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,60 @@ public function testDelegateNew()
155155
'12345abcD'
156156
);
157157

158+
//Testing that addresses from order and the order itself are assigned
159+
//to customer.
160+
$order = $this->orderRepository->get($orderId);
161+
$this->assertCount(1, $createdCustomer->getAddresses());
162+
$this->assertNotNull($createdCustomer->getDefaultBilling());
163+
$this->assertNotNull($createdCustomer->getDefaultShipping());
164+
foreach ($createdCustomer->getAddresses() as $address) {
165+
$this->assertTrue(
166+
$address->isDefaultBilling() || $address->isDefaultShipping()
167+
);
168+
if ($address->isDefaultBilling()) {
169+
$this->compareAddresses($order->getBillingAddress(), $address);
170+
} elseif ($address->isDefaultShipping()) {
171+
$this->compareAddresses($order->getShippingAddress(), $address);
172+
}
173+
}
174+
$this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
175+
}
176+
177+
/**
178+
* @magentoDbIsolation enabled
179+
* @magentoAppIsolation enabled
180+
* @magentoDataFixture Magento/Sales/_files/order_different_addresses.php
181+
*/
182+
public function testDelegateNewDifferentAddresses()
183+
{
184+
$orderAutoincrementId = '100000001';
185+
/** @var Order $orderModel */
186+
$orderModel = $this->orderFactory->create();
187+
$orderModel->loadByIncrementId($orderAutoincrementId);
188+
$orderId = $orderModel->getId();
189+
unset($orderModel);
190+
191+
$this->delegate->delegateNew($orderId);
192+
193+
//Saving new customer with prepared data from order.
194+
/** @var CustomerInterface $customer */
195+
$customer = $this->customerFactory->create();
196+
$customer->setWebsiteId(1)
197+
->setEmail('customer_order_delegate@example.com')
198+
->setGroupId(1)
199+
->setStoreId(1)
200+
->setPrefix('Mr.')
201+
->setFirstname('John')
202+
->setMiddlename('A')
203+
->setLastname('Smith')
204+
->setSuffix('Esq.')
205+
->setTaxvat('12')
206+
->setGender(0);
207+
$createdCustomer = $this->accountManagement->createAccount(
208+
$customer,
209+
'12345abcD'
210+
);
211+
158212
//Testing that addresses from order and the order itself are assigned
159213
//to customer.
160214
$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)