Skip to content

Commit 1b3987d

Browse files
committed
Revert "AC-15054: Product Add to Cart issue in Rest API"
This reverts commit 1df163e.
1 parent 62b18ee commit 1b3987d

File tree

4 files changed

+120
-341
lines changed

4 files changed

+120
-341
lines changed

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

Lines changed: 30 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\Exception\NoSuchEntityException;
1414
use Magento\Framework\Webapi\Rest\Request as RestRequest;
15-
use Magento\Quote\Api\CartRepositoryInterface;
1615
use Magento\Quote\Api\Data\CartItemInterface;
1716
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
1817
use Magento\Quote\Model\QuoteIdMaskFactory;
1918
use Magento\Store\Model\StoreManagerInterface;
2019

2120
/**
22-
* Plugin to update cart ID from request and validate product website assignment
21+
* Update cart id from request param
2322
*/
2423
class UpdateCartId
2524
{
@@ -28,42 +27,42 @@ class UpdateCartId
2827
* @param ProductRepositoryInterface $productRepository
2928
* @param StoreManagerInterface $storeManager
3029
* @param QuoteIdMaskFactory $quoteIdMaskFactory
31-
* @param CartRepositoryInterface $cartRepository
3230
*/
3331
public function __construct(
3432
private readonly RestRequest $request,
3533
private readonly ProductRepositoryInterface $productRepository,
36-
private readonly StoreManagerInterface $storeManager,
37-
private readonly QuoteIdMaskFactory $quoteIdMaskFactory,
38-
private readonly CartRepositoryInterface $cartRepository
34+
private readonly StoreManagerInterface $storeManager,
35+
private readonly QuoteIdMaskFactory $quoteIdMaskFactory
3936
) {
4037
}
4138

4239
/**
43-
* Before saving a guest cart item, set quote ID from request and validate website assignment
40+
* Update id from request if param cartId exist
4441
*
45-
* @param GuestCartItemRepositoryInterface $subject
42+
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
4643
* @param CartItemInterface $cartItem
47-
* @return void
48-
* @throws LocalizedException
49-
*
44+
* @return array
5045
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5146
*/
5247
public function beforeSave(
53-
GuestCartItemRepositoryInterface $subject,
48+
GuestCartItemRepositoryInterface $guestCartItemRepository,
5449
CartItemInterface $cartItem
55-
): void {
56-
if ($cartId = $this->request->getParam('cartId')) {
50+
): array {
51+
$cartId = $this->request->getParam('cartId');
52+
53+
if ($cartId) {
5754
$cartItem->setQuoteId($cartId);
5855
}
59-
6056
$this->validateProductWebsiteAssignment($cartItem);
57+
return [$cartItem];
6158
}
6259

6360
/**
64-
* Validate product's website assignment for guest cart item
61+
* Validate that product is assigned to the current website
6562
*
63+
* @param CartItemInterface $cartItem
6664
* @throws LocalizedException
65+
* @throws NoSuchEntityException
6766
*/
6867
private function validateProductWebsiteAssignment(CartItemInterface $cartItem): void
6968
{
@@ -72,84 +71,29 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
7271
return;
7372
}
7473

74+
// Get current website ID from the masked cart ID
7575
$maskedQuoteId = $cartItem->getQuoteId();
7676
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedQuoteId, 'masked_id');
77-
$quoteId = $quoteIdMask->getQuoteId();
7877

79-
if (!$quoteId) {
78+
if (!$quoteIdMask->getQuoteId()) {
8079
return;
8180
}
82-
83-
try {
84-
$quote = $this->cartRepository->get($quoteId);
85-
$storeId = $quote->getStoreId();
86-
87-
foreach ($quote->getAllItems() as $item) {
88-
if ($sku === $item->getSku()) {
89-
$this->validateWebsiteAssignment($item->getProductId(), $storeId);
90-
return;
91-
}
92-
}
93-
94-
// Product not in quote yet
95-
$this->validateWebsiteAssignmentBySku($sku, $storeId);
96-
97-
} catch (NoSuchEntityException) {
98-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
99-
}
100-
}
101-
102-
/**
103-
* Validate by SKU for new items
104-
*
105-
* @param string $sku
106-
* @param int $storeId
107-
* @return void
108-
* @throws LocalizedException
109-
*/
110-
private function validateWebsiteAssignmentBySku(string $sku, int $storeId): void
111-
{
112-
try {
113-
$product = $this->productRepository->get($sku, false, $storeId);
114-
$this->checkProductInWebsite($product->getWebsiteIds(), $storeId);
115-
} catch (NoSuchEntityException) {
116-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
117-
}
118-
}
119-
120-
/**
121-
* Validate by product ID for existing items
122-
*
123-
* @param int $productId
124-
* @param int $storeId
125-
* @return void
126-
* @throws LocalizedException
127-
*/
128-
private function validateWebsiteAssignment(int $productId, int $storeId): void
129-
{
81+
$currentWebsiteId = $this->storeManager->getStore()->getWebsiteId();
13082
try {
131-
$product = $this->productRepository->getById($productId, false, $storeId);
132-
$this->checkProductInWebsite($product->getWebsiteIds(), $storeId);
133-
} catch (NoSuchEntityException) {
134-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
135-
}
136-
}
83+
$product = $this->productRepository->get($sku, false, null);
13784

138-
/**
139-
* Validate by product ID for existing items
140-
*
141-
* @param array|null $websiteIds
142-
* @param int $storeId
143-
* @return void
144-
* @throws LocalizedException
145-
* @throws NoSuchEntityException
146-
*/
147-
private function checkProductInWebsite(?array $websiteIds, int $storeId): void
148-
{
149-
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
85+
$productWebsiteIds = $product->getWebsiteIds();
15086

151-
if (empty($websiteIds) || !in_array($websiteId, $websiteIds, true)) {
152-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
87+
// Validate website assignment
88+
if (!is_array($productWebsiteIds) || !in_array($currentWebsiteId, $productWebsiteIds)) {
89+
throw new LocalizedException(
90+
__('Product that you are trying to add is not available.')
91+
);
92+
}
93+
} catch (NoSuchEntityException $e) {
94+
throw new LocalizedException(
95+
__('Product that you are trying to add is not available.')
96+
);
15397
}
15498
}
15599
}

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

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -37,85 +37,55 @@ public function __construct(
3737
*
3838
* @param CartItemRepositoryInterface $subject
3939
* @param CartItemInterface $cartItem
40-
* @return void
40+
* @return array
4141
* @throws LocalizedException
42+
* @throws NoSuchEntityException
4243
*
4344
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4445
*/
4546
public function beforeSave(
4647
CartItemRepositoryInterface $subject,
4748
CartItemInterface $cartItem
48-
): void {
49-
$sku = $cartItem->getSku();
50-
if (!$sku) {
51-
return;
52-
}
53-
54-
try {
55-
$quote = $this->cartRepository->getActive($cartItem->getQuoteId());
56-
57-
foreach ($quote->getAllItems() as $item) {
58-
if ($sku === $item->getSku()) {
59-
$this->checkProductWebsiteAssignment($item->getProductId(), $item->getStoreId());
60-
return;
61-
}
62-
}
63-
64-
// Fallback: product not in quote items yet
65-
$this->checkProductWebsiteAssignmentBySku($sku, $quote->getStoreId());
66-
67-
} catch (NoSuchEntityException $e) {
68-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
69-
}
49+
): array {
50+
$this->validateProductWebsiteAssignment($cartItem);
51+
return [$cartItem];
7052
}
7153

7254
/**
73-
* Check product website assignment by SKU
55+
* Validate that product is assigned to the current website
7456
*
75-
* @param string $sku
76-
* @param int $storeId
57+
* @param CartItemInterface $cartItem
7758
* @throws LocalizedException
59+
* @throws NoSuchEntityException
7860
*/
79-
private function checkProductWebsiteAssignmentBySku(string $sku, int $storeId): void
61+
private function validateProductWebsiteAssignment(CartItemInterface $cartItem): void
8062
{
81-
try {
82-
$product = $this->productRepository->get($sku, false, $storeId);
83-
$this->validateWebsiteAssignment($product->getWebsiteIds(), $storeId);
84-
} catch (NoSuchEntityException $e) {
85-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
63+
$sku = $cartItem->getSku();
64+
if (!$sku) {
65+
return;
8666
}
87-
}
8867

89-
/**
90-
* Check product website assignment by product ID
91-
*
92-
* @param int $productId
93-
* @param int|null $storeId
94-
* @throws LocalizedException
95-
*/
96-
private function checkProductWebsiteAssignment($productId, $storeId): void
97-
{
68+
// Get current website ID from the cart's store
69+
$quote = $this->cartRepository->get($cartItem->getQuoteId());
70+
$storeId = $quote->getStoreId();
71+
$currentWebsiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
72+
9873
try {
99-
$product = $this->productRepository->getById($productId, false, $storeId);
100-
$this->validateWebsiteAssignment($product->getWebsiteIds(), $storeId);
101-
} catch (NoSuchEntityException $e) {
102-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
103-
}
104-
}
74+
// Load product to check website assignment
75+
$product = $this->productRepository->get($sku, false, $storeId);
10576

106-
/**
107-
* Validate product website assignment
108-
*
109-
* @param array|null $websiteIds
110-
* @param int $storeId
111-
* @throws LocalizedException
112-
*/
113-
private function validateWebsiteAssignment(?array $websiteIds, int $storeId): void
114-
{
115-
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
77+
$productWebsiteIds = $product->getWebsiteIds();
11678

117-
if (empty($websiteIds) || !in_array($websiteId, $websiteIds, true)) {
118-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
79+
// Validate website assignment
80+
if (!is_array($productWebsiteIds) || !in_array($currentWebsiteId, $productWebsiteIds)) {
81+
throw new LocalizedException(
82+
__('Product that you are trying to add is not available.')
83+
);
84+
}
85+
} catch (NoSuchEntityException $e) {
86+
throw new LocalizedException(
87+
__('Product that you are trying to add is not available.')
88+
);
11989
}
12090
}
12191
}

0 commit comments

Comments
 (0)