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