Skip to content

Commit cccc7f5

Browse files
committed
AC-15054: Fix Performance Acceptance Tests
1 parent 62448b8 commit cccc7f5

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

app/code/Magento/Quote/Plugin/UpdateCartId.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
1818
use Magento\Quote\Model\QuoteIdMaskFactory;
1919
use Magento\Store\Model\StoreManagerInterface;
20+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
21+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
2022

2123
/**
2224
* Plugin to update cart ID from request and validate product website assignment
@@ -35,7 +37,9 @@ public function __construct(
3537
private readonly ProductRepositoryInterface $productRepository,
3638
private readonly StoreManagerInterface $storeManager,
3739
private readonly QuoteIdMaskFactory $quoteIdMaskFactory,
38-
private readonly CartRepositoryInterface $cartRepository
40+
private readonly CartRepositoryInterface $cartRepository,
41+
private readonly ProductResource $productResource,
42+
private readonly ProductWebsiteLink $productWebsiteLink
3943
) {
4044
}
4145

@@ -74,23 +78,23 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
7478
return;
7579
}
7680

77-
$storeId = (int)($cartItem->getStoreId() ?? 0);
78-
if (!$storeId) {
79-
$maskedQuoteId = $cartItem->getQuoteId();
80-
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedQuoteId, 'masked_id');
81-
$quoteId = (int)$quoteIdMask->getQuoteId();
82-
if (!$quoteId) {
83-
return;
84-
}
85-
try {
86-
$quote = $this->cartRepository->get($quoteId);
87-
$storeId = (int)$quote->getStoreId();
88-
} catch (NoSuchEntityException) {
89-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
90-
}
81+
$maskedQuoteId = $cartItem->getQuoteId();
82+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedQuoteId, 'masked_id');
83+
$quoteId = $quoteIdMask->getQuoteId();
84+
85+
if (!$quoteId) {
86+
return;
9187
}
9288

93-
$this->validateWebsiteAssignmentBySku($sku, $storeId);
89+
try {
90+
$quote = $this->cartRepository->get($quoteId);
91+
$storeId = $quote->getStoreId();
92+
// Product not in quote yet
93+
$this->validateWebsiteAssignmentBySku($sku, $storeId);
94+
95+
} catch (NoSuchEntityException) {
96+
throw new LocalizedException(__('Product that you are trying to add is not available.'));
97+
}
9498
}
9599

96100
/**
@@ -103,12 +107,12 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
103107
*/
104108
private function validateWebsiteAssignmentBySku(string $sku, int $storeId): void
105109
{
106-
try {
107-
$product = $this->productRepository->get($sku, false, $storeId);
108-
$this->checkProductInWebsite($product->getWebsiteIds(), $storeId);
109-
} catch (NoSuchEntityException) {
110+
$productId = (int)$this->productResource->getIdBySku($sku);
111+
if (!$productId) {
110112
throw new LocalizedException(__('Product that you are trying to add is not available.'));
111113
}
114+
$websiteIds = $this->productWebsiteLink->getWebsiteIdsByProductId($productId);
115+
$this->checkProductInWebsite($websiteIds, $storeId);
112116
}
113117

114118
/**

app/code/Magento/Quote/Plugin/Webapi/ValidateProductWebsiteAssignment.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Magento\Quote\Api\CartRepositoryInterface;
1515
use Magento\Quote\Api\Data\CartItemInterface;
1616
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
18+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
1719

1820
/**
1921
* Plugin to validate product website assignment for REST API cart operations
@@ -28,7 +30,9 @@ class ValidateProductWebsiteAssignment
2830
public function __construct(
2931
private readonly ProductRepositoryInterface $productRepository,
3032
private readonly StoreManagerInterface $storeManager,
31-
private readonly CartRepositoryInterface $cartRepository
33+
private readonly CartRepositoryInterface $cartRepository,
34+
private readonly ProductResource $productResource,
35+
private readonly ProductWebsiteLink $productWebsiteLink
3236
) {
3337
}
3438

@@ -51,17 +55,13 @@ public function beforeSave(
5155
return;
5256
}
5357

54-
$storeId = (int)($cartItem->getStoreId() ?? 0);
55-
if (!$storeId) {
56-
try {
57-
$quote = $this->cartRepository->getActive($cartItem->getQuoteId());
58-
$storeId = (int)$quote->getStoreId();
59-
} catch (NoSuchEntityException $e) {
60-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
61-
}
62-
}
58+
try {
59+
$quote = $this->cartRepository->getActive($cartItem->getQuoteId());
60+
$this->checkProductWebsiteAssignmentBySku($sku, $quote->getStoreId());
6361

64-
$this->checkProductWebsiteAssignmentBySku($sku, $storeId);
62+
} catch (NoSuchEntityException $e) {
63+
throw new LocalizedException(__('Product that you are trying to add is not available.'));
64+
}
6565
}
6666

6767
/**
@@ -73,12 +73,12 @@ public function beforeSave(
7373
*/
7474
private function checkProductWebsiteAssignmentBySku(string $sku, int $storeId): void
7575
{
76-
try {
77-
$product = $this->productRepository->get($sku, false, $storeId);
78-
$this->validateWebsiteAssignment($product->getWebsiteIds(), $storeId);
79-
} catch (NoSuchEntityException $e) {
76+
$productId = (int)$this->productResource->getIdBySku($sku);
77+
if (!$productId) {
8078
throw new LocalizedException(__('Product that you are trying to add is not available.'));
8179
}
80+
$websiteIds = $this->productWebsiteLink->getWebsiteIdsByProductId($productId);
81+
$this->validateWebsiteAssignment($websiteIds, $storeId);
8282
}
8383

8484
/**

0 commit comments

Comments
 (0)