Skip to content

Commit 7fb673c

Browse files
committed
MC-32201: Reorder functionality
1 parent b595774 commit 7fb673c

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Sales\Model\Reorder;
88

99
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\Product;
1011
use Magento\Framework\Exception\InputException;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213
use Magento\Quote\Api\CartRepositoryInterface;
@@ -36,6 +37,7 @@ class Reorder
3637
* List of error messages and codes.
3738
*/
3839
private const MESSAGE_CODES = [
40+
'The required options you selected are not available' => self::ERROR_NOT_SALABLE,
3941
'Product that you are trying to add is not available' => self::ERROR_NOT_SALABLE,
4042
'This product is out of stock' => self::ERROR_NOT_SALABLE,
4143
'The fewest you may purchase is' => self::ERROR_INSUFFICIENT_STOCK,
@@ -161,6 +163,7 @@ private function addOrderItem(\Magento\Quote\Model\Quote $cart, $orderItem): voi
161163
$info->setQty($orderItem->getQtyOrdered());
162164

163165
try {
166+
/** @var Product $product */
164167
$product = $this->productRepository->getById($orderItem->getProductId(), false, null, true);
165168
} catch (NoSuchEntityException $e) {
166169
$this->addError(
@@ -169,13 +172,22 @@ private function addOrderItem(\Magento\Quote\Model\Quote $cart, $orderItem): voi
169172
);
170173
return;
171174
}
175+
$addProductResult = null;
172176
try {
173-
$cart->addProduct($product, $info);
177+
$addProductResult = $cart->addProduct($product, $info);
174178
} catch (\Magento\Framework\Exception\LocalizedException $e) {
175-
$this->addError($this->addCartItemError($product->getSku(), $e->getMessage()));
179+
$this->addError($this->getCartItemErrorMessage($orderItem, $product, $e->getMessage()));
176180
} catch (\Throwable $e) {
177181
$this->logger->critical($e);
178-
$this->addError($this->addCartItemError($product->getSku()), self::ERROR_UNDEFINED);
182+
$this->addError($this->getCartItemErrorMessage($orderItem, $product), self::ERROR_UNDEFINED);
183+
}
184+
185+
// error happens in case the result is string
186+
if (is_string($addProductResult)) {
187+
$errors = array_unique(explode("\n", $addProductResult));
188+
foreach ($errors as $error) {
189+
$this->addError($this->getCartItemErrorMessage($orderItem, $product, $error));
190+
}
179191
}
180192
}
181193
}
@@ -234,14 +246,18 @@ private function prepareOutput(CartInterface $cart): Data\ReorderOutput
234246
}
235247

236248
/**
237-
* Add error message for a cart item
249+
* Get error message for a cart item
238250
*
239-
* @param string $sku
251+
* @param Item $item
252+
* @param Product $product
240253
* @param string|null $message
241254
* @return string
242255
*/
243-
private function addCartItemError(string $sku, string $message = null): string
256+
private function getCartItemErrorMessage(Item $item, Product $product, string $message = null): string
244257
{
258+
// try to get sku from line-item first.
259+
// for complex product type: if custom option is not available it can cause error
260+
$sku = $item->getSku() ?? $product->getData('sku');
245261
return (string)($message
246262
? __('Could not add the product with SKU "%1" to the shopping cart: %2', $sku, $message)
247263
: __('Could not add the product with SKU "%1" to the shopping cart', $sku));

0 commit comments

Comments
 (0)