Skip to content

Commit c55816b

Browse files
committed
MC-41981: [Magento Cloud] Reorder Not Working On Storefront
1 parent 176cf49 commit c55816b

File tree

2 files changed

+81
-42
lines changed

2 files changed

+81
-42
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\Sales\Model\Reorder;
8+
9+
use Magento\Framework\DataObject;
10+
use Magento\Sales\Api\Data\OrderItemInterface;
11+
use Magento\Framework\Serialize\SerializerInterface;
12+
use Psr\Log\LoggerInterface;
13+
14+
/**
15+
* Gets info buy request from order info interface and process custom options
16+
*/
17+
class OrderInfoBuyRequestGetter
18+
{
19+
/**
20+
* @var LoggerInterface
21+
*/
22+
private $logger;
23+
24+
/**
25+
* @var SerializerInterface
26+
*/
27+
private $serializer;
28+
29+
/**
30+
* @param LoggerInterface $logger
31+
* @param SerializerInterface $serializer
32+
*/
33+
public function __construct(
34+
LoggerInterface $logger,
35+
SerializerInterface $serializer
36+
) {
37+
$this->logger = $logger;
38+
$this->serializer = $serializer;
39+
}
40+
41+
/**
42+
* Prepare Custom Option for order Item by unserializing custom options data
43+
*
44+
* @param OrderItemInterface $orderItem
45+
* @return DataObject
46+
*/
47+
public function getInfoBuyRequest(OrderItemInterface $orderItem): DataObject
48+
{
49+
$info = $orderItem->getProductOptionByCode('info_buyRequest');
50+
$options = $orderItem->getProductOptionByCode('options');
51+
52+
if (!empty($options) || is_array($info['options'])) {
53+
foreach ($options as $option) {
54+
if (array_key_exists($option['option_id'], $info['options'])) {
55+
try {
56+
$value = $this->serializer->unserialize($option['option_value']);
57+
$info['options'][$option['option_id']] = $value;
58+
} catch (\InvalidArgumentException $exception) {
59+
$this->logger->warning($exception);
60+
}
61+
}
62+
}
63+
}
64+
65+
$infoBuyRequest = new DataObject($info);
66+
$infoBuyRequest->setQty($orderItem->getQtyOrdered());
67+
68+
return $infoBuyRequest;
69+
}
70+
71+
}

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

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Magento\Sales\Model\OrderFactory;
2323
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
2424
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
25-
use \Magento\Framework\Serialize\SerializerInterface;
25+
use Psr\Log\LoggerInterface;
2626

2727
/**
2828
* Allows customer quickly to reorder previously added products and put them to the Cart
@@ -64,7 +64,7 @@ class Reorder
6464
private $reorderHelper;
6565

6666
/**
67-
* @var \Psr\Log\LoggerInterface
67+
* @var LoggerInterface
6868
*/
6969
private $logger;
7070

@@ -94,28 +94,29 @@ class Reorder
9494
private $guestCartResolver;
9595

9696
/**
97-
* @var SerializerInterface
97+
* @var OrderInfoBuyRequestGetter
9898
*/
99-
private $serializer;
99+
private $orderInfoBuyRequestGetter;
100100

101101
/**
102102
* @param OrderFactory $orderFactory
103103
* @param CustomerCartResolver $customerCartProvider
104104
* @param GuestCartResolver $guestCartResolver
105105
* @param CartRepositoryInterface $cartRepository
106106
* @param ReorderHelper $reorderHelper
107-
* @param \Psr\Log\LoggerInterface $logger
107+
* @param LoggerInterface $logger
108108
* @param ProductCollectionFactory $productCollectionFactory
109+
* @param OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
109110
*/
110111
public function __construct(
111112
OrderFactory $orderFactory,
112113
CustomerCartResolver $customerCartProvider,
113114
GuestCartResolver $guestCartResolver,
114115
CartRepositoryInterface $cartRepository,
115116
ReorderHelper $reorderHelper,
116-
\Psr\Log\LoggerInterface $logger,
117+
LoggerInterface $logger,
117118
ProductCollectionFactory $productCollectionFactory,
118-
\Magento\Framework\Serialize\SerializerInterface $serializer
119+
OrderInfoBuyRequestGetter $orderInfoBuyRequestGetter
119120
) {
120121
$this->orderFactory = $orderFactory;
121122
$this->cartRepository = $cartRepository;
@@ -124,7 +125,7 @@ public function __construct(
124125
$this->customerCartProvider = $customerCartProvider;
125126
$this->guestCartResolver = $guestCartResolver;
126127
$this->productCollectionFactory = $productCollectionFactory;
127-
$this->serializer = $serializer;
128+
$this->orderInfoBuyRequestGetter = $orderInfoBuyRequestGetter;
128129
}
129130

130131
/**
@@ -251,9 +252,7 @@ private function getOrderProducts(string $storeId, array $orderItemProductIds):
251252
*/
252253
private function addItemToCart(OrderItemInterface $orderItem, Quote $cart, ProductInterface $product): void
253254
{
254-
$infoBuyRequest = $this->getInfoBuyRequest($orderItem);
255-
$infoBuyRequest = new \Magento\Framework\DataObject($infoBuyRequest );
256-
$infoBuyRequest->setQty($orderItem->getQtyOrdered());
255+
$infoBuyRequest = $this->orderInfoBuyRequestGetter->getInfoBuyRequest($orderItem);
257256

258257
$addProductResult = null;
259258
try {
@@ -346,35 +345,4 @@ private function getCartItemErrorMessage(Item $item, Product $product, string $m
346345
? __('Could not add the product with SKU "%1" to the shopping cart: %2', $sku, $message)
347346
: __('Could not add the product with SKU "%1" to the shopping cart', $sku));
348347
}
349-
350-
/**
351-
* Prepare Custom Option for order Item
352-
*
353-
* @param OrderItemInterface $orderItem
354-
* @return array|null
355-
*/
356-
private function getInfoBuyRequest(OrderItemInterface $orderItem): ?array
357-
{
358-
$info = $orderItem->getProductOptionByCode('info_buyRequest');
359-
$options = $orderItem->getProductOptionByCode('options');
360-
361-
if (empty($options) || !is_array($info['options'])) {
362-
return $info;
363-
}
364-
365-
foreach ($options as $option) {
366-
if (array_key_exists($option['option_id'], $info['options'])) {
367-
try {
368-
$value = $this->serializer->unserialize($option['option_value']);
369-
$info['options'][$option['option_id']] = $value;
370-
} catch (\InvalidArgumentException $exception) {
371-
//log the exception as warning
372-
$this->_logger->warning($exception);
373-
}
374-
}
375-
}
376-
377-
return $info;
378-
}
379-
380348
}

0 commit comments

Comments
 (0)