Skip to content

Commit aa98a38

Browse files
committed
ACP2E-279: Customer details are lost after editing an order.
1 parent a46e4f2 commit aa98a38

File tree

2 files changed

+12
-191
lines changed

2 files changed

+12
-191
lines changed

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,12 +1833,20 @@ protected function _validateCustomerData(\Magento\Customer\Api\Data\CustomerInte
18331833
*/
18341834
public function _prepareCustomer()
18351835
{
1836-
if ($this->getQuote()->getCustomerIsGuest()) {
1837-
return $this;
1838-
}
18391836
/** @var $store \Magento\Store\Model\Store */
18401837
$store = $this->getSession()->getStore();
18411838
$customer = $this->getQuote()->getCustomer();
1839+
if ($this->getQuote()->getCustomerIsGuest()) {
1840+
$customerBillingAddressDataObject = $this->getBillingAddress()->exportCustomerAddress();
1841+
$customer->setFirstname($customerBillingAddressDataObject->getFirstname())
1842+
->setLastname($customerBillingAddressDataObject->getLastname())
1843+
->setMiddlename($customerBillingAddressDataObject->getMiddlename())
1844+
->setStoreId($store->getId())
1845+
->setWebsiteId($store->getWebsiteId());
1846+
$customer = $this->_validateCustomerData($customer);
1847+
$this->getQuote()->setCustomer($customer);
1848+
return $this;
1849+
}
18421850

18431851
if ($customer->getId() && !$this->_customerIsInStore($store)) {
18441852
/** Create a new customer record if it is not available in the specified store */
@@ -2008,7 +2016,6 @@ public function createOrder()
20082016
$oldOrder->setRelationChildId($order->getId());
20092017
$oldOrder->setRelationChildRealId($order->getIncrementId());
20102018
$oldOrder->save();
2011-
$this->updateGuestName($oldOrder, $order);
20122019
$this->orderManagement->cancel($oldOrder->getEntityId());
20132020
$order->save();
20142021
}
@@ -2021,25 +2028,6 @@ public function createOrder()
20212028
return $order;
20222029
}
20232030

2024-
/**
2025-
* Update guest name after create order
2026-
*
2027-
* @param Order $oldOrder
2028-
* @param Order $order
2029-
* @return Order
2030-
*/
2031-
private function updateGuestName(Order $oldOrder, Order $order): Order
2032-
{
2033-
if ($order->getCustomerIsGuest()) {
2034-
$order->setCustomerFirstname($oldOrder->getCustomerFirstname());
2035-
$order->setCustomerLastname($oldOrder->getCustomerLastname());
2036-
if ($oldOrder->getMiddlename() === null) {
2037-
$order->setCustomerMiddlename($oldOrder->getCustomerMiddlename());
2038-
}
2039-
}
2040-
return $order;
2041-
}
2042-
20432031
/**
20442032
* Validate quote data before order creation
20452033
*

app/code/Magento/Sales/Test/Unit/Model/AdminOrder/CreateTest.php

Lines changed: 1 addition & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,9 @@
2020
use Magento\Framework\App\RequestInterface;
2121
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
2222
use Magento\Quote\Api\CartRepositoryInterface;
23-
use Magento\Quote\Api\CartManagementInterface;
2423
use Magento\Quote\Model\Quote;
25-
use Magento\Quote\Model\Quote\Payment;
26-
use Magento\Payment\Model\MethodInterface;
2724
use Magento\Quote\Model\Quote\Address;
2825
use Magento\Quote\Model\Quote\Item;
29-
use Magento\Catalog\Model\Product;
30-
use Magento\Catalog\Model\Product\Type\AbstractType;
3126
use Magento\Quote\Model\Quote\Item\Updater;
3227
use Magento\Quote\Model\QuoteFactory;
3328
use Magento\Sales\Model\AdminOrder\Create;
@@ -44,7 +39,7 @@
4439
*/
4540
class CreateTest extends TestCase
4641
{
47-
private const CUSTOMER_ID = 1;
42+
const CUSTOMER_ID = 1;
4843

4944
/**
5045
* @var Create
@@ -56,11 +51,6 @@ class CreateTest extends TestCase
5651
*/
5752
private $quoteRepository;
5853

59-
/**
60-
* @var CartManagementInterface|MockObject
61-
*/
62-
private $quoteManagement;
63-
6454
/**
6555
* @var QuoteFactory|MockObject
6656
*/
@@ -114,10 +104,6 @@ protected function setUp(): void
114104
$this->formFactory = $this->createPartialMock(FormFactory::class, ['create']);
115105
$this->quoteFactory = $this->createPartialMock(QuoteFactory::class, ['create']);
116106
$this->customerFactory = $this->createPartialMock(CustomerInterfaceFactory::class, ['create']);
117-
$this->quoteManagement = $this->getMockBuilder(CartManagementInterface::class)
118-
->disableOriginalConstructor()
119-
->setMethods(['submit'])
120-
->getMockForAbstractClass();
121107

122108
$this->itemUpdater = $this->createMock(Updater::class);
123109

@@ -131,7 +117,6 @@ protected function setUp(): void
131117
->setMethods(
132118
[
133119
'getQuote',
134-
'getOrder',
135120
'getStoreId',
136121
'getCustomerId',
137122
'setData',
@@ -149,8 +134,6 @@ protected function setUp(): void
149134
$storeMock = $this->getMockBuilder(StoreInterface::class)
150135
->setMethods(['getId'])
151136
->getMockForAbstractClass();
152-
$storeMock->method('getId')
153-
->willReturn(1);
154137
$this->sessionQuote->method('getStore')
155138
->willReturn($storeMock);
156139

@@ -178,15 +161,6 @@ protected function setUp(): void
178161
'getShippingAddress',
179162
'getBillingAddress',
180163
'getCouponCode',
181-
'getCustomerFirstname',
182-
'getCustomerLastname',
183-
'getCustomerMiddlename',
184-
'getIncrementId',
185-
'getOriginalIncrementId',
186-
'getEditIncrement',
187-
'setRelationChildId',
188-
'setRelationChildRealId',
189-
'save',
190164
]
191165
)
192166
->getMock();
@@ -204,7 +178,6 @@ protected function setUp(): void
204178
'dataObjectHelper' => $this->dataObjectHelper,
205179
'quoteRepository' => $this->quoteRepository,
206180
'quoteFactory' => $this->quoteFactory,
207-
'quoteManagement' => $this->quoteManagement,
208181
]
209182
);
210183
}
@@ -488,144 +461,4 @@ public function testInitFromOrder()
488461

489462
$this->adminOrderCreate->initFromOrder($this->orderMock);
490463
}
491-
492-
/**
493-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
494-
*/
495-
public function testCreateOrder()
496-
{
497-
$method = $this->getMockBuilder(MethodInterface::class)
498-
->disableOriginalConstructor()
499-
->setMethods(
500-
[
501-
'isAvailable',
502-
'validate',
503-
]
504-
)
505-
->getMockForAbstractClass();
506-
$method->method('isAvailable')
507-
->willReturn(true);
508-
$method->method('validate')
509-
->willReturn(true);
510-
$payment = $this->getMockBuilder(Payment::class)
511-
->disableOriginalConstructor()
512-
->setMethods(
513-
[
514-
'getMethod',
515-
'getMethodInstance',
516-
]
517-
)
518-
->getMock();
519-
$payment->method('getMethod')
520-
->willReturn('checkmo');
521-
$payment->method('getMethodInstance')
522-
->willReturn($method);
523-
524-
$type = $this->getMockBuilder(AbstractType::class)
525-
->disableOriginalConstructor()
526-
->setMethods(
527-
[
528-
'getOrderOptions',
529-
]
530-
)
531-
->getMockForAbstractClass();
532-
$type->method('getOrderOptions')
533-
->willReturn(false);
534-
535-
$product = $this->getMockBuilder(Product::class)
536-
->disableOriginalConstructor()
537-
->setMethods(
538-
[
539-
'getTypeInstance',
540-
]
541-
)
542-
->getMockForAbstractClass();
543-
$product->method('getTypeInstance')
544-
->willReturn($type);
545-
546-
$item = $this->getMockBuilder(Item::class)
547-
->disableOriginalConstructor()
548-
->setMethods(
549-
[
550-
'getHasError',
551-
'getProduct',
552-
'getOptionByCode',
553-
]
554-
)
555-
->getMockForAbstractClass();
556-
$item->method('getHasError')
557-
->willReturn(false);
558-
$item->method('getProduct')
559-
->willReturn($product);
560-
$item->method('getOptionByCode')
561-
->willReturn(false);
562-
$items = [
563-
$item
564-
];
565-
566-
$quote = $this->getMockBuilder(Quote::class)
567-
->disableOriginalConstructor()
568-
->setMethods(
569-
[
570-
'getCustomerIsGuest',
571-
'getAllItems',
572-
'isVirtual',
573-
'getPayment',
574-
'getItemById',
575-
]
576-
)
577-
->getMock();
578-
$quote->method('getCustomerIsGuest')
579-
->willReturn(true);
580-
$quote->method('getAllItems')
581-
->willReturn($items);
582-
$quote->method('isVirtual')
583-
->willReturn(true);
584-
$quote->method('getPayment')
585-
->willReturn($payment);
586-
$quote->method('getItemById')
587-
->willReturn($item);
588-
589-
$this->sessionQuote
590-
->method('getQuote')
591-
->willReturn($quote);
592-
$this->orderMock->method('getId')
593-
->willReturn(1);
594-
$this->orderMock->method('getCustomerFirstname')
595-
->willReturn('firstname');
596-
$this->orderMock->method('getCustomerLastname')
597-
->willReturn('lastname');
598-
$this->orderMock->method('getCustomerMiddlename')
599-
->willReturn('middlename');
600-
$this->orderMock->method('getIncrementId')
601-
->willReturn('100000001');
602-
$this->orderMock->method('getEditIncrement')
603-
->willReturn(0);
604-
$this->sessionQuote
605-
->method('getOrder')
606-
->willReturn($this->orderMock);
607-
608-
$guestOrder = $this->getMockBuilder(Order::class)
609-
->disableOriginalConstructor()
610-
->setMethods(
611-
[
612-
'getId',
613-
'getIncrementId',
614-
'getCustomerIsGuest',
615-
'save',
616-
]
617-
)
618-
->getMock();
619-
$guestOrder->method('getId')
620-
->willReturn(2);
621-
$guestOrder->method('getIncrementId')
622-
->willReturn('100000001-1');
623-
$guestOrder->method('getCustomerIsGuest')
624-
->willReturn(true);
625-
$this->quoteManagement->method('submit')
626-
->willReturn($guestOrder);
627-
628-
$object = $this->adminOrderCreate->createOrder();
629-
self::assertEquals($this->orderMock->getCustomerFirstname(), $object->getCustomerFirstname());
630-
}
631464
}

0 commit comments

Comments
 (0)