Skip to content

Commit e1f475d

Browse files
authored
Merge pull request #7762 from magento-arcticfoxes/B2B-2379
B2B-2379: Cart is being loaded twice in multiple graphql mutation
2 parents 0d04908 + 7b1feab commit e1f475d

File tree

8 files changed

+24
-10
lines changed

8 files changed

+24
-10
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\Quote\Model\Cart;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Framework\App\ObjectManager;
1113
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Quote\Api\CartRepositoryInterface;
1315
use Magento\Quote\Model\Cart\BuyRequest\BuyRequestBuilder;
@@ -65,22 +67,32 @@ class AddProductsToCart
6567
*/
6668
private $requestBuilder;
6769

70+
/**
71+
* @var SearchCriteriaBuilder
72+
*/
73+
private $searchCriteriaBuilder;
74+
6875
/**
6976
* @param ProductRepositoryInterface $productRepository
7077
* @param CartRepositoryInterface $cartRepository
7178
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
7279
* @param BuyRequestBuilder $requestBuilder
80+
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
7381
*/
7482
public function __construct(
7583
ProductRepositoryInterface $productRepository,
7684
CartRepositoryInterface $cartRepository,
7785
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
78-
BuyRequestBuilder $requestBuilder
86+
BuyRequestBuilder $requestBuilder,
87+
SearchCriteriaBuilder $searchCriteriaBuilder = null
7988
) {
8089
$this->productRepository = $productRepository;
8190
$this->cartRepository = $cartRepository;
8291
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
8392
$this->requestBuilder = $requestBuilder;
93+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()->get(
94+
SearchCriteriaBuilder::class
95+
);
8496
}
8597

8698
/**
@@ -145,6 +157,16 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
145157
public function addItemsToCart(Quote $cart, array $cartItems): array
146158
{
147159
$failedCartItems = [];
160+
$cartItemSkus = \array_map(
161+
function ($item) {
162+
return $item->getSku();
163+
},
164+
$cartItems
165+
);
166+
167+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $cartItemSkus, 'in')->create();
168+
// getList() call caches product models in runtime cache
169+
$this->productRepository->getList($searchCriteria)->getItems();
148170

149171
foreach ($cartItems as $cartItemPosition => $cartItem) {
150172
$errors = $this->addItemToCart($cart, $cartItem, $cartItemPosition);
@@ -178,7 +200,7 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
178200
);
179201
} else {
180202
try {
181-
$product = $this->productRepository->get($sku, false, null, true);
203+
$product = $this->productRepository->get($sku, false, $cart->getStoreId(), false);
182204
} catch (NoSuchEntityException $e) {
183205
$errors[] = $this->createError(
184206
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),

app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ private function run($context, ?array $args): array
9090
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
9191
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
9292
$this->addProductsToCart->execute($cart, $cartItems);
93-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
9493

9594
return [
9695
'cart' => [

app/code/Magento/QuoteGraphQl/Model/Resolver/ApplyCouponToCart.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8585
throw new LocalizedException(__($e->getMessage()), $e);
8686
}
8787

88-
$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId, $storeId);
8988
return [
9089
'cart' => [
9190
'model' => $cart,

app/code/Magento/QuoteGraphQl/Model/Resolver/SetBillingAddressOnCart.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setBillingAddressOnCart->execute($context, $cart, $billingAddress);
72-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7372

7473
return [
7574
'cart' => [

app/code/Magento/QuoteGraphQl/Model/Resolver/SetPaymentMethodOnCart.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setPaymentMethodOnCart->execute($cart, $paymentData);
72-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7372

7473
return [
7574
'cart' => [

app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingAddressesOnCart.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setShippingAddressesOnCart->execute($context, $cart, $shippingAddresses);
72-
// reload updated cart
73-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7472

7573
return [
7674
'cart' => [

app/code/Magento/QuoteGraphQl/Model/Resolver/SetShippingMethodsOnCart.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6969
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7070
$this->checkCartCheckoutAllowance->execute($cart);
7171
$this->setShippingMethodsOnCart->execute($context, $cart, $shippingMethods);
72-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
7372

7473
return [
7574
'cart' => [

app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9494
throw new GraphQlInputException(__($e->getMessage()), $e);
9595
}
9696

97-
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
9897
return [
9998
'cart' => [
10099
'model' => $cart,

0 commit comments

Comments
 (0)