Skip to content

Commit 535758b

Browse files
committed
Revert "AC-15054: Product Add to Cart issue in Rest API"
This reverts commit 4374e7f.
1 parent f8acf62 commit 535758b

File tree

3 files changed

+17
-326
lines changed

3 files changed

+17
-326
lines changed
Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +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;
1512
use Magento\Quote\Api\Data\CartItemInterface;
1613
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
17-
use Magento\Quote\Model\QuoteIdMaskFactory;
18-
use Magento\Store\Model\StoreManagerInterface;
1914

2015
/**
2116
* Update cart id from request param
2217
*/
2318
class UpdateCartId
2419
{
20+
/**
21+
* @var RestRequest $request
22+
*/
23+
private $request;
24+
2525
/**
2626
* @param RestRequest $request
27-
* @param ProductRepositoryInterface $productRepository
28-
* @param StoreManagerInterface $storeManager
29-
* @param QuoteIdMaskFactory $quoteIdMaskFactory
3027
*/
31-
public function __construct(
32-
private readonly RestRequest $request,
33-
private readonly ProductRepositoryInterface $productRepository,
34-
private readonly StoreManagerInterface $storeManager,
35-
private readonly QuoteIdMaskFactory $quoteIdMaskFactory
36-
) {
28+
public function __construct(RestRequest $request)
29+
{
30+
$this->request = $request;
3731
}
3832

3933
/**
4034
* Update id from request if param cartId exist
4135
*
4236
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
4337
* @param CartItemInterface $cartItem
44-
* @return array
38+
* @return void
4539
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4640
*/
4741
public function beforeSave(
4842
GuestCartItemRepositoryInterface $guestCartItemRepository,
4943
CartItemInterface $cartItem
50-
): array {
44+
): void {
5145
$cartId = $this->request->getParam('cartId');
5246

5347
if ($cartId) {
5448
$cartItem->setQuoteId($cartId);
5549
}
56-
$this->validateProductWebsiteAssignment($cartItem);
57-
return [$cartItem];
58-
}
59-
60-
/**
61-
* Validate that product is assigned to the current website
62-
*
63-
* @param CartItemInterface $cartItem
64-
* @throws LocalizedException
65-
* @throws NoSuchEntityException
66-
*/
67-
private function validateProductWebsiteAssignment(CartItemInterface $cartItem): void
68-
{
69-
$sku = $cartItem->getSku();
70-
if (!$sku) {
71-
return;
72-
}
73-
74-
// Get current website ID from the masked cart ID
75-
$maskedQuoteId = $cartItem->getQuoteId();
76-
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedQuoteId, 'masked_id');
77-
78-
if (!$quoteIdMask->getQuoteId()) {
79-
return;
80-
}
81-
$currentWebsiteId = $this->storeManager->getStore()->getWebsiteId();
82-
try {
83-
$product = $this->productRepository->get($sku, false, null);
84-
85-
$productWebsiteIds = $product->getWebsiteIds();
86-
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-
);
97-
}
9850
}
9951
}

app/code/Magento/Quote/etc/webapi_rest/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<type name="Magento\Quote\Model\QuoteValidator">
2424
<plugin name="error_redirect_processor" type="Magento\Quote\Plugin\Webapi\Model\ErrorRedirectProcessor" />
2525
</type>
26+
<type name="Magento\Quote\Api\GuestCartItemRepositoryInterface">
27+
<plugin name="validateProductWebsiteAssignmentGuest"
28+
type="Magento\Quote\Plugin\Webapi\ValidateProductWebsiteAssignmentForGuest"
29+
sortOrder="100" />
30+
</type>
2631
<type name="Magento\Quote\Api\CartItemRepositoryInterface">
2732
<plugin name="validateProductWebsiteAssignment"
2833
type="Magento\Quote\Plugin\Webapi\ValidateProductWebsiteAssignment"

dev/tests/api-functional/testsuite/Magento/Quote/Api/ValidateProductWebsiteAssignmentTest.php

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

0 commit comments

Comments
 (0)