|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Copyright © Magento, Inc. All rights reserved. |
4 | | - * See COPYING.txt for license details. |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */ |
6 | 6 |
|
7 | 7 | declare(strict_types=1); |
8 | 8 |
|
9 | 9 | namespace Magento\Quote\Plugin; |
10 | 10 |
|
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Framework\Exception\NoSuchEntityException; |
11 | 14 | use Magento\Framework\Webapi\Rest\Request as RestRequest; |
| 15 | +use Magento\Quote\Api\CartRepositoryInterface; |
12 | 16 | use Magento\Quote\Api\Data\CartItemInterface; |
13 | 17 | use Magento\Quote\Api\GuestCartItemRepositoryInterface; |
| 18 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 19 | +use Magento\Store\Model\StoreManagerInterface; |
14 | 20 |
|
15 | 21 | /** |
16 | | - * Update cart id from request param |
| 22 | + * Plugin to update cart ID from request and validate product website assignment |
17 | 23 | */ |
18 | 24 | class UpdateCartId |
19 | 25 | { |
20 | | - /** |
21 | | - * @var RestRequest $request |
22 | | - */ |
23 | | - private $request; |
24 | | - |
25 | 26 | /** |
26 | 27 | * @param RestRequest $request |
| 28 | + * @param ProductRepositoryInterface $productRepository |
| 29 | + * @param StoreManagerInterface $storeManager |
| 30 | + * @param QuoteIdMaskFactory $quoteIdMaskFactory |
| 31 | + * @param CartRepositoryInterface $cartRepository |
27 | 32 | */ |
28 | | - public function __construct(RestRequest $request) |
29 | | - { |
30 | | - $this->request = $request; |
| 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 | + ) { |
31 | 40 | } |
32 | 41 |
|
33 | 42 | /** |
34 | | - * Update id from request if param cartId exist |
| 43 | + * Before saving a guest cart item, set quote ID from request and validate website assignment |
35 | 44 | * |
36 | | - * @param GuestCartItemRepositoryInterface $guestCartItemRepository |
| 45 | + * @param GuestCartItemRepositoryInterface $subject |
37 | 46 | * @param CartItemInterface $cartItem |
38 | 47 | * @return void |
| 48 | + * @throws LocalizedException |
| 49 | + * |
39 | 50 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
40 | 51 | */ |
41 | 52 | public function beforeSave( |
42 | | - GuestCartItemRepositoryInterface $guestCartItemRepository, |
| 53 | + GuestCartItemRepositoryInterface $subject, |
43 | 54 | CartItemInterface $cartItem |
44 | 55 | ): void { |
45 | | - $cartId = $this->request->getParam('cartId'); |
46 | | - |
47 | | - if ($cartId) { |
| 56 | + if ($cartId = $this->request->getParam('cartId')) { |
48 | 57 | $cartItem->setQuoteId($cartId); |
49 | 58 | } |
| 59 | + |
| 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.')); |
| 158 | + } |
50 | 159 | } |
51 | 160 | } |
0 commit comments