Skip to content

Commit eb81692

Browse files
committed
ACP2E-2213: Loading order through repository loads the order multiple times from DB
1 parent dd3c6e7 commit eb81692

File tree

3 files changed

+91
-37
lines changed

3 files changed

+91
-37
lines changed

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
*/
66
namespace Magento\Sales\Model\Order;
77

8-
use Magento\Sales\Api\Data\OrderInterface;
98
use Magento\Sales\Api\Data\ShippingAssignmentInterface;
109
use Magento\Sales\Api\Data\ShippingAssignmentInterfaceFactory;
1110
use Magento\Sales\Model\OrderFactory;
11+
use Magento\Sales\Api\Data\OrderInterface;
1212

13-
/**
14-
* Class ShippingAssignmentBuilder
15-
* @package Magento\Sales\Model\Order
16-
*/
1713
class ShippingAssignmentBuilder
1814
{
1915
/**
@@ -31,6 +27,11 @@ class ShippingAssignmentBuilder
3127
*/
3228
private $shippingBuilderFactory;
3329

30+
/**
31+
* @var int|null
32+
*/
33+
private $orderId = null;
34+
3435
/**
3536
* @var OrderInterface
3637
*/
@@ -54,46 +55,68 @@ public function __construct(
5455
}
5556

5657
/**
58+
* Setter for orderId property
59+
*
5760
* @param int $orderId
5861
* @return void
5962
*/
6063
public function setOrderId($orderId)
6164
{
62-
$this->order = $this->orderFactory->create()->load($orderId);
65+
$this->orderId = $orderId;
6366
}
6467

6568
/**
69+
* Setter for order property
70+
*
6671
* @param OrderInterface $order
6772
* @return void
6873
*/
6974
public function setOrder(OrderInterface $order)
7075
{
7176
$this->order = $order;
77+
$this->orderId = $order->getEntityId();
7278
}
7379

7480
/**
81+
* Getter for orderId property
82+
*
83+
* @return int|null
84+
*/
85+
private function getOrderId()
86+
{
87+
return $this->orderId;
88+
}
89+
90+
/**
91+
* Get order
92+
*
7593
* @return OrderInterface
7694
*/
77-
private function getOrder()
95+
private function getOrder() : OrderInterface
7896
{
97+
if ($this->order === null) {
98+
$this->order = $this->orderFactory->create()->load($this->getOrderId());
99+
}
79100
return $this->order;
80101
}
81102

82103
/**
104+
* Create shipment assignement
105+
*
83106
* @return ShippingAssignmentInterface[]|null
84107
*/
85108
public function create()
86109
{
87110
$shippingAssignments = null;
88-
if ($order = $this->getOrder()) {
111+
if ($this->getOrderId()) {
89112
/** @var ShippingAssignmentInterface $shippingAssignment */
90113
$shippingAssignment = $this->shippingAssignmentFactory->create();
91114

92115
$shipping = $this->shippingBuilderFactory->create();
93-
$shipping->setOrder($order);
116+
$shipping->setOrder($this->getOrder());
94117
$shippingAssignment->setShipping($shipping->create());
95-
$shippingAssignment->setItems($order->getItems());
96-
$shippingAssignment->setStockId($order->getStockId());
118+
$shippingAssignment->setItems($this->getOrder()->getItems());
119+
$shippingAssignment->setStockId($this->getOrder()->getStockId());
97120
//for now order has only one shipping assignment
98121
$shippingAssignments = [$shippingAssignment];
99122
}

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

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
*/
66
namespace Magento\Sales\Model\Order;
77

8-
use Magento\Sales\Api\Data\OrderInterface;
98
use Magento\Sales\Api\Data\ShippingInterface;
109
use Magento\Sales\Api\Data\ShippingInterfaceFactory;
1110
use Magento\Sales\Api\Data\TotalInterface;
1211
use Magento\Sales\Api\Data\TotalInterfaceFactory;
13-
use Magento\Sales\Model\Order;
12+
use Magento\Sales\Api\Data\OrderInterface;
1413
use Magento\Sales\Model\OrderFactory;
1514

16-
/**
17-
* Class ShippingBuilder
18-
* @package Magento\Sales\Model\Order
19-
*/
2015
class ShippingBuilder
2116
{
17+
/**
18+
* @var int|null
19+
*/
20+
private $orderId = null;
21+
2222
/**
2323
* @var OrderInterface
2424
*/
@@ -57,52 +57,77 @@ public function __construct(
5757
}
5858

5959
/**
60+
* Setter for orderId property
61+
*
6062
* @param int $orderId
6163
* @return void
6264
*/
6365
public function setOrderId($orderId)
6466
{
65-
$this->order = $this->orderFactory->create()->load($orderId);
67+
$this->orderId = $orderId;
6668
}
6769

6870
/**
71+
* Setter for order property
72+
*
6973
* @param OrderInterface $order
7074
* @return void
7175
*/
7276
public function setOrder(OrderInterface $order)
7377
{
7478
$this->order = $order;
79+
$this->orderId = $order->getEntityId();
7580
}
7681

7782
/**
78-
* @return OrderInterface
79-
*/
80-
private function getOrder()
81-
{
82-
return $this->order;
83-
}
84-
85-
/**
83+
* Create shipping
84+
*
8685
* @return ShippingInterface|null
8786
*/
8887
public function create()
8988
{
9089
$shipping = null;
91-
$order = $this->getOrder();
92-
if ($order && $order->getEntityId()) {
93-
/** @var ShippingInterface $shipping */
94-
$shipping = $this->shippingFactory->create();
95-
$shippingAddress = $order->getShippingAddress();
96-
if ($shippingAddress) {
97-
$shipping->setAddress($shippingAddress);
90+
if ($this->getOrderId()) {
91+
if ($this->getOrder()->getEntityId()) {
92+
/** @var ShippingInterface $shipping */
93+
$shipping = $this->shippingFactory->create();
94+
$shippingAddress = $this->order->getShippingAddress();
95+
if ($shippingAddress) {
96+
$shipping->setAddress($shippingAddress);
97+
}
98+
$shipping->setMethod($this->order->getShippingMethod());
99+
$shipping->setTotal($this->getTotal());
98100
}
99-
$shipping->setMethod($order->getShippingMethod());
100-
$shipping->setTotal($this->getTotal());
101101
}
102102
return $shipping;
103103
}
104104

105105
/**
106+
* Getter for the $orderId property
107+
*
108+
* @return int|null
109+
*/
110+
private function getOrderId()
111+
{
112+
return $this->orderId;
113+
}
114+
115+
/**
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+
*
106131
* @return TotalInterface
107132
*/
108133
private function getTotal()

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,14 @@ public function save(\Magento\Sales\Api\Data\OrderInterface $entity)
276276
$shippingAssignments = $extensionAttributes->getShippingAssignments();
277277
if (!empty($shippingAssignments)) {
278278
$shipping = array_shift($shippingAssignments)->getShipping();
279-
$entity->setShippingAddress($shipping->getAddress());
280-
$entity->setShippingMethod($shipping->getMethod());
279+
$shippingAddress = $shipping->getAddress();
280+
$shippingEmail = $shippingAddress->getEmail();
281+
$shippingMethod = $shipping->getMethod();
282+
$entity->setShippingAddress($shippingAddress);
283+
$entity->setShippingMethod($shippingMethod);
284+
if (!$entity->getCustomerEmail() && $shippingEmail) {
285+
$entity->setCustomerEmail($shippingEmail);
286+
}
281287
}
282288
}
283289

0 commit comments

Comments
 (0)