Skip to content

Commit 42fe02b

Browse files
committed
Merge branch 'ACP2E-2213' of https://github.com/magento-l3/magento2ce into PR-VK-2023-09-29
2 parents 3ee3962 + 5602924 commit 42fe02b

File tree

6 files changed

+265
-43
lines changed

6 files changed

+265
-43
lines changed

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
use Magento\Sales\Api\Data\ShippingAssignmentInterface;
99
use Magento\Sales\Api\Data\ShippingAssignmentInterfaceFactory;
1010
use Magento\Sales\Model\OrderFactory;
11+
use Magento\Sales\Api\Data\OrderInterface;
1112

12-
/**
13-
* Class ShippingAssignmentBuilder
14-
* @package Magento\Sales\Model\Order
15-
*/
1613
class ShippingAssignmentBuilder
1714
{
1815
/**
@@ -35,6 +32,11 @@ class ShippingAssignmentBuilder
3532
*/
3633
private $orderId = null;
3734

35+
/**
36+
* @var OrderInterface
37+
*/
38+
private $order;
39+
3840
/**
3941
* ShippingAssignment constructor.
4042
*
@@ -53,6 +55,8 @@ public function __construct(
5355
}
5456

5557
/**
58+
* Setter for orderId property
59+
*
5660
* @param int $orderId
5761
* @return void
5862
*/
@@ -62,6 +66,20 @@ public function setOrderId($orderId)
6266
}
6367

6468
/**
69+
* Setter for order property
70+
*
71+
* @param OrderInterface $order
72+
* @return void
73+
*/
74+
public function setOrder(OrderInterface $order)
75+
{
76+
$this->order = $order;
77+
$this->orderId = $order->getEntityId();
78+
}
79+
80+
/**
81+
* Getter for orderId property
82+
*
6583
* @return int|null
6684
*/
6785
private function getOrderId()
@@ -70,21 +88,35 @@ private function getOrderId()
7088
}
7189

7290
/**
91+
* Get order
92+
*
93+
* @return OrderInterface
94+
*/
95+
private function getOrder() : OrderInterface
96+
{
97+
if ($this->order === null) {
98+
$this->order = $this->orderFactory->create()->load($this->getOrderId());
99+
}
100+
return $this->order;
101+
}
102+
103+
/**
104+
* Create shipment assignement
105+
*
73106
* @return ShippingAssignmentInterface[]|null
74107
*/
75108
public function create()
76109
{
77110
$shippingAssignments = null;
78111
if ($this->getOrderId()) {
79-
$order = $this->orderFactory->create()->load($this->getOrderId());
80112
/** @var ShippingAssignmentInterface $shippingAssignment */
81113
$shippingAssignment = $this->shippingAssignmentFactory->create();
82114

83115
$shipping = $this->shippingBuilderFactory->create();
84-
$shipping->setOrderId($this->getOrderId());
116+
$shipping->setOrder($this->getOrder());
85117
$shippingAssignment->setShipping($shipping->create());
86-
$shippingAssignment->setItems($order->getItems());
87-
$shippingAssignment->setStockId($order->getStockId());
118+
$shippingAssignment->setItems($this->getOrder()->getItems());
119+
$shippingAssignment->setStockId($this->getOrder()->getStockId());
88120
//for now order has only one shipping assignment
89121
$shippingAssignments = [$shippingAssignment];
90122
}

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@
99
use Magento\Sales\Api\Data\ShippingInterfaceFactory;
1010
use Magento\Sales\Api\Data\TotalInterface;
1111
use Magento\Sales\Api\Data\TotalInterfaceFactory;
12-
use Magento\Sales\Model\Order;
12+
use Magento\Sales\Api\Data\OrderInterface;
1313
use Magento\Sales\Model\OrderFactory;
1414

15-
/**
16-
* Class ShippingBuilder
17-
* @package Magento\Sales\Model\Order
18-
*/
1915
class ShippingBuilder
2016
{
2117
/**
@@ -24,7 +20,7 @@ class ShippingBuilder
2420
private $orderId = null;
2521

2622
/**
27-
* @var Order
23+
* @var OrderInterface
2824
*/
2925
private $order;
3026

@@ -61,6 +57,8 @@ public function __construct(
6157
}
6258

6359
/**
60+
* Setter for orderId property
61+
*
6462
* @param int $orderId
6563
* @return void
6664
*/
@@ -70,14 +68,27 @@ public function setOrderId($orderId)
7068
}
7169

7270
/**
71+
* Setter for order property
72+
*
73+
* @param OrderInterface $order
74+
* @return void
75+
*/
76+
public function setOrder(OrderInterface $order)
77+
{
78+
$this->order = $order;
79+
$this->orderId = $order->getEntityId();
80+
}
81+
82+
/**
83+
* Create shipping
84+
*
7385
* @return ShippingInterface|null
7486
*/
7587
public function create()
7688
{
7789
$shipping = null;
7890
if ($this->getOrderId()) {
79-
$this->order = $this->orderFactory->create()->load($this->getOrderId());
80-
if ($this->order->getEntityId()) {
91+
if ($this->getOrder()->getEntityId()) {
8192
/** @var ShippingInterface $shipping */
8293
$shipping = $this->shippingFactory->create();
8394
$shippingAddress = $this->order->getShippingAddress();
@@ -92,6 +103,8 @@ public function create()
92103
}
93104

94105
/**
106+
* Getter for the $orderId property
107+
*
95108
* @return int|null
96109
*/
97110
private function getOrderId()
@@ -100,6 +113,21 @@ private function getOrderId()
100113
}
101114

102115
/**
116+
* Get order by id
117+
*
118+
* @return OrderInterface
119+
*/
120+
private function getOrder() : OrderInterface
121+
{
122+
if ($this->order === null) {
123+
$this->order = $this->orderFactory->create()->load($this->getOrderId());
124+
}
125+
return $this->order;
126+
}
127+
128+
/**
129+
* Create total
130+
*
103131
* @return TotalInterface
104132
*/
105133
private function getTotal()

app/code/Magento/Sales/Model/OrderRepository.php

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ class OrderRepository implements \Magento\Sales\Api\OrderRepositoryInterface, Re
9393
* @param OrderTaxManagementInterface|null $orderTaxManagement
9494
* @param PaymentAdditionalInfoInterfaceFactory|null $paymentAdditionalInfoFactory
9595
* @param JsonSerializer|null $serializer
96-
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
96+
* @param JoinProcessorInterface|null $extensionAttributesJoinProcessor
97+
* @param ShippingAssignmentBuilder|null $shippingAssignmentBuilder
9798
*/
9899
public function __construct(
99100
Metadata $metadata,
@@ -103,7 +104,8 @@ public function __construct(
103104
OrderTaxManagementInterface $orderTaxManagement = null,
104105
PaymentAdditionalInfoInterfaceFactory $paymentAdditionalInfoFactory = null,
105106
JsonSerializer $serializer = null,
106-
JoinProcessorInterface $extensionAttributesJoinProcessor = null
107+
JoinProcessorInterface $extensionAttributesJoinProcessor = null,
108+
ShippingAssignmentBuilder $shippingAssignmentBuilder = null
107109
) {
108110
$this->metadata = $metadata;
109111
$this->searchResultFactory = $searchResultFactory;
@@ -119,6 +121,8 @@ public function __construct(
119121
->get(JsonSerializer::class);
120122
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor
121123
?: ObjectManager::getInstance()->get(JoinProcessorInterface::class);
124+
$this->shippingAssignmentBuilder = $shippingAssignmentBuilder
125+
?: ObjectManager::getInstance()->get(ShippingAssignmentBuilder::class);
122126
}
123127

124128
/**
@@ -276,8 +280,14 @@ public function save(\Magento\Sales\Api\Data\OrderInterface $entity)
276280
$shippingAssignments = $extensionAttributes->getShippingAssignments();
277281
if (!empty($shippingAssignments)) {
278282
$shipping = array_shift($shippingAssignments)->getShipping();
279-
$entity->setShippingAddress($shipping->getAddress());
280-
$entity->setShippingMethod($shipping->getMethod());
283+
$shippingAddress = $shipping->getAddress();
284+
$shippingEmail = ($shippingAddress !== null) ? $shippingAddress->getEmail() : null;
285+
$shippingMethod = $shipping->getMethod();
286+
$entity->setShippingAddress($shippingAddress);
287+
$entity->setShippingMethod($shippingMethod);
288+
if (!$entity->getCustomerEmail() && $shippingEmail) {
289+
$entity->setCustomerEmail($shippingEmail);
290+
}
281291
}
282292
}
283293

@@ -302,29 +312,11 @@ private function setShippingAssignments(OrderInterface $order)
302312
} elseif ($extensionAttributes->getShippingAssignments() !== null) {
303313
return;
304314
}
305-
/** @var ShippingAssignmentInterface $shippingAssignment */
306-
$shippingAssignments = $this->getShippingAssignmentBuilderDependency();
307-
$shippingAssignments->setOrderId($order->getEntityId());
308-
$extensionAttributes->setShippingAssignments($shippingAssignments->create());
315+
$this->shippingAssignmentBuilder->setOrder($order);
316+
$extensionAttributes->setShippingAssignments($this->shippingAssignmentBuilder->create());
309317
$order->setExtensionAttributes($extensionAttributes);
310318
}
311319

312-
/**
313-
* Get the new ShippingAssignmentBuilder dependency for application code
314-
*
315-
* @return ShippingAssignmentBuilder
316-
* @deprecated 100.0.4
317-
*/
318-
private function getShippingAssignmentBuilderDependency()
319-
{
320-
if (!$this->shippingAssignmentBuilder instanceof ShippingAssignmentBuilder) {
321-
$this->shippingAssignmentBuilder = \Magento\Framework\App\ObjectManager::getInstance()->get(
322-
\Magento\Sales\Model\Order\ShippingAssignmentBuilder::class
323-
);
324-
}
325-
return $this->shippingAssignmentBuilder;
326-
}
327-
328320
/**
329321
* Helper function that adds a FilterGroup to the collection.
330322
*
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Test\Unit\Model\Order;
9+
10+
use Magento\Sales\Api\Data\ShippingAssignmentInterface;
11+
use Magento\Sales\Api\Data\ShippingAssignmentInterfaceFactory;
12+
use Magento\Sales\Model\OrderFactory;
13+
use Magento\Sales\Api\Data\OrderInterface;
14+
use Magento\Sales\Model\Order\ShippingAssignmentBuilder;
15+
use Magento\Sales\Model\Order\ShippingBuilderFactory;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ShippingAssignmentBuilderTest extends TestCase
20+
{
21+
/**
22+
* @var ShippingAssignmentBuilder
23+
*/
24+
private $shippingAssignmentBuilder;
25+
26+
/**
27+
* @var OrderFactory|MockObject
28+
*/
29+
private $orderFactory;
30+
31+
/**
32+
* @var ShippingAssignmentInterfaceFactory|Mockobject
33+
*/
34+
private $shippingAssignmentFactory;
35+
36+
/**
37+
* @var ShippingBuilderFactory|MockObject
38+
*/
39+
private $shippingBuilderFactory;
40+
41+
/**
42+
* @inheirtDoc
43+
*/
44+
protected function setUp(): void
45+
{
46+
$this->orderFactory = $this->getMockBuilder(OrderFactory::class)
47+
->disableOriginalConstructor()
48+
->getMock();
49+
$this->shippingAssignmentFactory = $this->getMockBuilder(ShippingAssignmentInterfaceFactory::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
$this->shippingBuilderFactory = $this->getMockBuilder(ShippingBuilderFactory::class)
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$this->shippingAssignmentBuilder = new ShippingAssignmentBuilder(
56+
$this->orderFactory,
57+
$this->shippingAssignmentFactory,
58+
$this->shippingBuilderFactory
59+
);
60+
}
61+
62+
/**
63+
* Test for case when order is provided instead of order_id
64+
*
65+
* @return void
66+
*/
67+
public function testCreateWithOrder() : void
68+
{
69+
$order = $this->getMockBuilder(OrderInterface::class)
70+
->getMockForAbstractClass();
71+
$this->shippingAssignmentBuilder->setOrder($order);
72+
$order->expects($this->any())
73+
->method('getEntityId')
74+
->willReturn(1);
75+
$this->orderFactory->expects($this->never())
76+
->method('create');
77+
78+
$this->shippingAssignmentBuilder->create();
79+
}
80+
}

0 commit comments

Comments
 (0)