7
7
namespace Magento \Sales \Model \Reorder ;
8
8
9
9
use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Magento \Catalog \Model \Product ;
10
11
use Magento \Framework \Exception \InputException ;
11
12
use Magento \Framework \Exception \NoSuchEntityException ;
12
13
use Magento \Quote \Api \CartRepositoryInterface ;
@@ -36,6 +37,7 @@ class Reorder
36
37
* List of error messages and codes.
37
38
*/
38
39
private const MESSAGE_CODES = [
40
+ 'The required options you selected are not available ' => self ::ERROR_NOT_SALABLE ,
39
41
'Product that you are trying to add is not available ' => self ::ERROR_NOT_SALABLE ,
40
42
'This product is out of stock ' => self ::ERROR_NOT_SALABLE ,
41
43
'The fewest you may purchase is ' => self ::ERROR_INSUFFICIENT_STOCK ,
@@ -161,6 +163,7 @@ private function addOrderItem(\Magento\Quote\Model\Quote $cart, $orderItem): voi
161
163
$ info ->setQty ($ orderItem ->getQtyOrdered ());
162
164
163
165
try {
166
+ /** @var Product $product */
164
167
$ product = $ this ->productRepository ->getById ($ orderItem ->getProductId (), false , null , true );
165
168
} catch (NoSuchEntityException $ e ) {
166
169
$ this ->addError (
@@ -169,13 +172,22 @@ private function addOrderItem(\Magento\Quote\Model\Quote $cart, $orderItem): voi
169
172
);
170
173
return ;
171
174
}
175
+ $ addProductResult = null ;
172
176
try {
173
- $ cart ->addProduct ($ product , $ info );
177
+ $ addProductResult = $ cart ->addProduct ($ product , $ info );
174
178
} catch (\Magento \Framework \Exception \LocalizedException $ e ) {
175
- $ this ->addError ($ this ->addCartItemError ( $ product-> getSku () , $ e ->getMessage ()));
179
+ $ this ->addError ($ this ->getCartItemErrorMessage ( $ orderItem , $ product , $ e ->getMessage ()));
176
180
} catch (\Throwable $ e ) {
177
181
$ 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
+ }
179
191
}
180
192
}
181
193
}
@@ -234,14 +246,18 @@ private function prepareOutput(CartInterface $cart): Data\ReorderOutput
234
246
}
235
247
236
248
/**
237
- * Add error message for a cart item
249
+ * Get error message for a cart item
238
250
*
239
- * @param string $sku
251
+ * @param Item $item
252
+ * @param Product $product
240
253
* @param string|null $message
241
254
* @return string
242
255
*/
243
- private function addCartItemError ( string $ sku , string $ message = null ): string
256
+ private function getCartItemErrorMessage ( Item $ item , Product $ product , string $ message = null ): string
244
257
{
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 ' );
245
261
return (string )($ message
246
262
? __ ('Could not add the product with SKU "%1" to the shopping cart: %2 ' , $ sku , $ message )
247
263
: __ ('Could not add the product with SKU "%1" to the shopping cart ' , $ sku ));
0 commit comments