Skip to content

Commit 4308cb5

Browse files
AnujNehraAnujNehra
authored andcommitted
ACP2E-1975: GraphQL cannot add product with customizable option to cart
1 parent 93d0e1d commit 4308cb5

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

app/code/Magento/Quote/Model/Cart/AddProductsToCart.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Quote\Model\Cart;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
1011
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Quote\Api\CartRepositoryInterface;
1213
use Magento\Quote\Model\Cart\BuyRequest\BuyRequestBuilder;
@@ -46,25 +47,33 @@ class AddProductsToCart
4647
*/
4748
private $error;
4849

50+
/**
51+
* @var ProductRepositoryInterface
52+
*/
53+
private $productRepository;
54+
4955
/**
5056
* @param CartRepositoryInterface $cartRepository
5157
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
5258
* @param BuyRequestBuilder $requestBuilder
5359
* @param ProductReaderInterface $productReader
5460
* @param AddProductsToCartError $addProductsToCartError
61+
* @param ProductRepositoryInterface $productRepository
5562
*/
5663
public function __construct(
5764
CartRepositoryInterface $cartRepository,
5865
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
5966
BuyRequestBuilder $requestBuilder,
6067
ProductReaderInterface $productReader,
61-
AddProductsToCartError $addProductsToCartError
68+
AddProductsToCartError $addProductsToCartError,
69+
ProductRepositoryInterface $productRepository
6270
) {
6371
$this->cartRepository = $cartRepository;
6472
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
6573
$this->requestBuilder = $requestBuilder;
6674
$this->productReader = $productReader;
6775
$this->error = $addProductsToCartError;
76+
$this->productRepository = $productRepository;
6877
}
6978

7079
/**
@@ -134,7 +143,6 @@ function ($item) {
134143
},
135144
$cartItems
136145
);
137-
$this->productReader->loadProducts($skus, $cart->getStoreId());
138146
foreach ($cartItems as $cartItemPosition => $cartItem) {
139147
$errors = $this->addItemToCart($cart, $cartItem, $cartItemPosition);
140148
if ($errors) {
@@ -165,7 +173,11 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
165173
$cartItemPosition
166174
);
167175
} else {
168-
$product = $this->productReader->getProductBySku($sku);
176+
try {
177+
$product = clone $this->productRepository->get($sku, false, $cart->getStoreId());
178+
} catch (NoSuchEntityException $e) {
179+
$product = null;
180+
}
169181
if (!$product || !$product->isSaleable() || !$product->isAvailable()) {
170182
$errors[] = $this->error->create(
171183
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),

app/code/Magento/Quote/Model/Quote.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,8 +1647,6 @@ public function addProduct(
16471647
);
16481648
}
16491649

1650-
$productId = $product->getId();
1651-
$product = clone $this->productRepository->getById($productId, false, $this->getStore()->getId());
16521650
$cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced($request, $product, $processMode);
16531651

16541652
/**

0 commit comments

Comments
 (0)