Skip to content

Commit f6503a2

Browse files
committed
AC-15054: Product Add to Cart issue in Rest API
Revert changes to fix PAT failure
1 parent 1ab8ce5 commit f6503a2

File tree

5 files changed

+17
-1019
lines changed

5 files changed

+17
-1019
lines changed
Lines changed: 17 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,51 @@
11
<?php
22
/**
3-
* Copyright 2025 Adobe
4-
* All Rights Reserved.
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
55
*/
66

77
declare(strict_types=1);
88

99
namespace Magento\Quote\Plugin;
1010

11-
use Magento\Catalog\Api\ProductRepositoryInterface;
12-
use Magento\Framework\Exception\LocalizedException;
13-
use Magento\Framework\Exception\NoSuchEntityException;
1411
use Magento\Framework\Webapi\Rest\Request as RestRequest;
15-
use Magento\Quote\Api\CartRepositoryInterface;
1612
use Magento\Quote\Api\Data\CartItemInterface;
1713
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
18-
use Magento\Quote\Model\QuoteIdMaskFactory;
19-
use Magento\Store\Model\StoreManagerInterface;
2014

2115
/**
22-
* Plugin to update cart ID from request and validate product website assignment
16+
* Update cart id from request param
2317
*/
2418
class UpdateCartId
2519
{
20+
/**
21+
* @var RestRequest $request
22+
*/
23+
private $request;
24+
2625
/**
2726
* @param RestRequest $request
28-
* @param ProductRepositoryInterface $productRepository
29-
* @param StoreManagerInterface $storeManager
30-
* @param QuoteIdMaskFactory $quoteIdMaskFactory
31-
* @param CartRepositoryInterface $cartRepository
3227
*/
33-
public function __construct(
34-
private readonly RestRequest $request,
35-
private readonly ProductRepositoryInterface $productRepository,
36-
private readonly StoreManagerInterface $storeManager,
37-
private readonly QuoteIdMaskFactory $quoteIdMaskFactory,
38-
private readonly CartRepositoryInterface $cartRepository
39-
) {
28+
public function __construct(RestRequest $request)
29+
{
30+
$this->request = $request;
4031
}
4132

4233
/**
43-
* Before saving a guest cart item, set quote ID from request and validate website assignment
34+
* Update id from request if param cartId exist
4435
*
45-
* @param GuestCartItemRepositoryInterface $subject
36+
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
4637
* @param CartItemInterface $cartItem
4738
* @return void
48-
* @throws LocalizedException
49-
*
5039
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5140
*/
5241
public function beforeSave(
53-
GuestCartItemRepositoryInterface $subject,
42+
GuestCartItemRepositoryInterface $guestCartItemRepository,
5443
CartItemInterface $cartItem
5544
): void {
56-
if ($cartId = $this->request->getParam('cartId')) {
57-
$cartItem->setQuoteId($cartId);
58-
}
45+
$cartId = $this->request->getParam('cartId');
5946

60-
$this->validateProductWebsiteAssignment($cartItem);
61-
}
62-
63-
/**
64-
* Validate product's website assignment for guest cart item
65-
*
66-
* @param CartItemInterface $cartItem
67-
* @return void
68-
* @throws LocalizedException
69-
*/
70-
private function validateProductWebsiteAssignment(CartItemInterface $cartItem): void
71-
{
72-
$sku = $cartItem->getSku();
73-
if (!$sku) {
74-
return;
75-
}
76-
77-
$maskedQuoteId = $cartItem->getQuoteId();
78-
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedQuoteId, 'masked_id');
79-
$quoteId = $quoteIdMask->getQuoteId();
80-
81-
if (!$quoteId) {
82-
return;
83-
}
84-
85-
try {
86-
$quote = $this->cartRepository->get($quoteId);
87-
$storeId = $quote->getStoreId();
88-
89-
foreach ($quote->getAllItems() as $item) {
90-
if ($sku === $item->getSku()) {
91-
$this->validateWebsiteAssignment($item->getProductId(), $storeId);
92-
return;
93-
}
94-
}
95-
96-
// Product not in quote yet
97-
$this->validateWebsiteAssignmentBySku($sku, $storeId);
98-
99-
} catch (NoSuchEntityException) {
100-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
101-
}
102-
}
103-
104-
/**
105-
* Validate by SKU for new items
106-
*
107-
* @param string $sku
108-
* @param int $storeId
109-
* @return void
110-
* @throws LocalizedException
111-
*/
112-
private function validateWebsiteAssignmentBySku(string $sku, int $storeId): void
113-
{
114-
try {
115-
$product = $this->productRepository->get($sku, false, $storeId);
116-
$this->checkProductInWebsite($product->getWebsiteIds(), $storeId);
117-
} catch (NoSuchEntityException) {
118-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
119-
}
120-
}
121-
122-
/**
123-
* Validate by product ID for existing items
124-
*
125-
* @param int $productId
126-
* @param int $storeId
127-
* @return void
128-
* @throws LocalizedException
129-
*/
130-
private function validateWebsiteAssignment(int $productId, int $storeId): void
131-
{
132-
try {
133-
$product = $this->productRepository->getById($productId, false, $storeId);
134-
if (empty($product->getWebsiteIds())) {
135-
return;
136-
}
137-
$this->checkProductInWebsite($product->getWebsiteIds(), $storeId);
138-
} catch (NoSuchEntityException) {
139-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
140-
}
141-
}
142-
143-
/**
144-
* Validate by product ID for existing items
145-
*
146-
* @param array|null $websiteIds
147-
* @param int $storeId
148-
* @return void
149-
* @throws LocalizedException
150-
* @throws NoSuchEntityException
151-
*/
152-
private function checkProductInWebsite(?array $websiteIds, int $storeId): void
153-
{
154-
$websiteId = $this->storeManager->getStore($storeId)->getWebsiteId();
155-
156-
if (empty($websiteIds) || !in_array($websiteId, $websiteIds, true)) {
157-
throw new LocalizedException(__('Product that you are trying to add is not available.'));
47+
if ($cartId) {
48+
$cartItem->setQuoteId($cartId);
15849
}
15950
}
16051
}

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

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)