|
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\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\Exception\NoSuchEntityException; |
11 | 13 | use Magento\Framework\Webapi\Rest\Request as RestRequest;
|
| 14 | +use Magento\Quote\Api\CartRepositoryInterface; |
12 | 15 | use Magento\Quote\Api\Data\CartItemInterface;
|
13 | 16 | use Magento\Quote\Api\GuestCartItemRepositoryInterface;
|
| 17 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 18 | +use Magento\Store\Model\StoreManagerInterface; |
| 19 | +use Magento\Catalog\Model\ResourceModel\Product as ProductResource; |
| 20 | +use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink; |
14 | 21 |
|
15 | 22 | /**
|
16 |
| - * Update cart id from request param |
| 23 | + * Plugin to update cart ID from request and validate product website assignment |
17 | 24 | */
|
18 | 25 | class UpdateCartId
|
19 | 26 | {
|
20 |
| - /** |
21 |
| - * @var RestRequest $request |
22 |
| - */ |
23 |
| - private $request; |
24 |
| - |
25 | 27 | /**
|
26 | 28 | * @param RestRequest $request
|
| 29 | + * @param StoreManagerInterface $storeManager |
| 30 | + * @param QuoteIdMaskFactory $quoteIdMaskFactory |
| 31 | + * @param CartRepositoryInterface $cartRepository |
| 32 | + * @param ProductResource $productResource |
| 33 | + * @param ProductWebsiteLink $productWebsiteLink |
27 | 34 | */
|
28 |
| - public function __construct(RestRequest $request) |
29 |
| - { |
30 |
| - $this->request = $request; |
| 35 | + public function __construct( |
| 36 | + private readonly RestRequest $request, |
| 37 | + private readonly StoreManagerInterface $storeManager, |
| 38 | + private readonly QuoteIdMaskFactory $quoteIdMaskFactory, |
| 39 | + private readonly CartRepositoryInterface $cartRepository, |
| 40 | + private readonly ProductResource $productResource, |
| 41 | + private readonly ProductWebsiteLink $productWebsiteLink |
| 42 | + ) { |
31 | 43 | }
|
32 | 44 |
|
33 | 45 | /**
|
34 |
| - * Update id from request if param cartId exist |
| 46 | + * Before saving a guest cart item, set quote ID from request and validate website assignment |
35 | 47 | *
|
36 |
| - * @param GuestCartItemRepositoryInterface $guestCartItemRepository |
| 48 | + * @param GuestCartItemRepositoryInterface $subject |
37 | 49 | * @param CartItemInterface $cartItem
|
38 | 50 | * @return void
|
| 51 | + * @throws LocalizedException |
| 52 | + * |
39 | 53 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
40 | 54 | */
|
41 | 55 | public function beforeSave(
|
42 |
| - GuestCartItemRepositoryInterface $guestCartItemRepository, |
| 56 | + GuestCartItemRepositoryInterface $subject, |
43 | 57 | CartItemInterface $cartItem
|
44 | 58 | ): void {
|
45 |
| - $cartId = $this->request->getParam('cartId'); |
46 |
| - |
47 |
| - if ($cartId) { |
| 59 | + if ($cartId = $this->request->getParam('cartId')) { |
48 | 60 | $cartItem->setQuoteId($cartId);
|
49 | 61 | }
|
| 62 | + |
| 63 | + $this->validateProductWebsiteAssignment($cartItem); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Validate product's website assignment for guest cart item |
| 68 | + * |
| 69 | + * @param CartItemInterface $cartItem |
| 70 | + * @return void |
| 71 | + * @throws LocalizedException |
| 72 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 73 | + */ |
| 74 | + private function validateProductWebsiteAssignment(CartItemInterface $cartItem): void |
| 75 | + { |
| 76 | + $sku = $cartItem->getSku(); |
| 77 | + if (!$sku) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Skip validation on update; website was already validated on add |
| 82 | + if ($cartItem->getItemId()) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $storeId = (int)($cartItem->getStoreId() ?? 0); |
| 87 | + |
| 88 | + if (!$storeId) { |
| 89 | + try { |
| 90 | + $storeId = (int)$this->storeManager->getStore()->getId(); |
| 91 | + } catch (\Throwable $e) { |
| 92 | + $storeId = 0; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (!$storeId) { |
| 97 | + try { |
| 98 | + $maskedQuoteId = $cartItem->getQuoteId(); |
| 99 | + if ($maskedQuoteId) { |
| 100 | + $quoteIdMask = $this->quoteIdMaskFactory->create()->load($maskedQuoteId, 'masked_id'); |
| 101 | + $quoteId = (int)$quoteIdMask->getQuoteId(); |
| 102 | + if ($quoteId) { |
| 103 | + $storeId = (int)$this->cartRepository->get($quoteId)->getStoreId(); |
| 104 | + } |
| 105 | + } |
| 106 | + } catch (NoSuchEntityException) { |
| 107 | + throw new LocalizedException(__('Product that you are trying to add is not available.')); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if ($storeId) { |
| 112 | + $this->validateWebsiteAssignmentBySku($sku, $storeId); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Validate by SKU for new items |
| 118 | + * |
| 119 | + * @param string $sku |
| 120 | + * @param int $storeId |
| 121 | + * @return void |
| 122 | + * @throws LocalizedException |
| 123 | + */ |
| 124 | + private function validateWebsiteAssignmentBySku(string $sku, int $storeId): void |
| 125 | + { |
| 126 | + $productId = (int)$this->productResource->getIdBySku($sku); |
| 127 | + if (!$productId) { |
| 128 | + throw new LocalizedException(__('Product that you are trying to add is not available.')); |
| 129 | + } |
| 130 | + $websiteIds = $this->productWebsiteLink->getWebsiteIdsByProductId($productId); |
| 131 | + if (empty($websiteIds)) { |
| 132 | + return; |
| 133 | + } |
| 134 | + $this->checkProductInWebsite($websiteIds, $storeId); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Validate by product ID for existing items |
| 139 | + * |
| 140 | + * @param array|null $websiteIds |
| 141 | + * @param int $storeId |
| 142 | + * @return void |
| 143 | + * @throws LocalizedException |
| 144 | + * @throws NoSuchEntityException |
| 145 | + */ |
| 146 | + private function checkProductInWebsite(?array $websiteIds, int $storeId): void |
| 147 | + { |
| 148 | + $websiteId = $this->storeManager->getStore($storeId)->getWebsiteId(); |
| 149 | + |
| 150 | + if (empty($websiteIds) || !in_array($websiteId, $websiteIds, true)) { |
| 151 | + throw new LocalizedException(__('Product that you are trying to add is not available.')); |
| 152 | + } |
50 | 153 | }
|
51 | 154 | }
|
0 commit comments