Skip to content

Commit 322a9e6

Browse files
committed
Revert "AC-15054: Product Add to Cart issue in Rest API"
This reverts commit 0d44645.
1 parent 535758b commit 322a9e6

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Plugin\Webapi;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Api\Data\CartItemInterface;
14+
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
15+
use Magento\Quote\Model\QuoteIdMaskFactory;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
18+
/**
19+
* Plugin to validate product website assignment for guest cart operations
20+
*/
21+
class ValidateProductWebsiteAssignmentForGuest
22+
{
23+
/**
24+
* @param ProductRepositoryInterface $productRepository
25+
* @param StoreManagerInterface $storeManager
26+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
27+
*/
28+
public function __construct(
29+
private readonly ProductRepositoryInterface $productRepository,
30+
private readonly StoreManagerInterface $storeManager,
31+
private readonly QuoteIdMaskFactory $quoteIdMaskFactory
32+
) {
33+
}
34+
35+
/**
36+
* Validate product website assignment before saving guest cart item
37+
*
38+
* @param GuestCartItemRepositoryInterface $subject
39+
* @param CartItemInterface $cartItem
40+
* @return array
41+
* @throws LocalizedException
42+
* @throws NoSuchEntityException
43+
*
44+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
45+
*/
46+
public function beforeSave(
47+
GuestCartItemRepositoryInterface $subject,
48+
CartItemInterface $cartItem
49+
): array {
50+
$this->validateProductWebsiteAssignment($cartItem);
51+
return [$cartItem];
52+
}
53+
54+
/**
55+
* Validate that product is assigned to the current website
56+
*
57+
* @param CartItemInterface $cartItem
58+
* @throws LocalizedException
59+
* @throws NoSuchEntityException
60+
*/
61+
private function validateProductWebsiteAssignment(CartItemInterface $cartItem): void
62+
{
63+
$sku = $cartItem->getSku();
64+
if (!$sku) {
65+
return;
66+
}
67+
68+
// Get current website ID from the masked cart ID
69+
$maskedQuoteId = $cartItem->getQuoteId();
70+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedQuoteId, 'masked_id');
71+
72+
if (!$quoteIdMask->getQuoteId()) {
73+
return; // Let other validations handle invalid cart
74+
}
75+
76+
$currentWebsiteId = $this->storeManager->getStore()->getWebsiteId();
77+
78+
try {
79+
$product = $this->productRepository->get($sku, false, null);
80+
81+
$productWebsiteIds = $product->getWebsiteIds();
82+
83+
// Validate website assignment
84+
if (!is_array($productWebsiteIds) || !in_array($currentWebsiteId, $productWebsiteIds)) {
85+
throw new LocalizedException(
86+
__('Product that you are trying to add is not available.')
87+
);
88+
}
89+
} catch (NoSuchEntityException $e) {
90+
throw new LocalizedException(
91+
__('Product that you are trying to add is not available.')
92+
);
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)