|
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 | 12 | use Magento\Quote\Api\Data\CartItemInterface;
|
16 | 13 | use Magento\Quote\Api\GuestCartItemRepositoryInterface;
|
17 |
| -use Magento\Quote\Model\QuoteIdMaskFactory; |
18 |
| -use Magento\Store\Model\StoreManagerInterface; |
19 | 14 |
|
20 | 15 | /**
|
21 | 16 | * Update cart id from request param
|
22 | 17 | */
|
23 | 18 | class UpdateCartId
|
24 | 19 | {
|
| 20 | + /** |
| 21 | + * @var RestRequest $request |
| 22 | + */ |
| 23 | + private $request; |
| 24 | + |
25 | 25 | /**
|
26 | 26 | * @param RestRequest $request
|
27 |
| - * @param ProductRepositoryInterface $productRepository |
28 |
| - * @param StoreManagerInterface $storeManager |
29 |
| - * @param QuoteIdMaskFactory $quoteIdMaskFactory |
30 | 27 | */
|
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; |
37 | 31 | }
|
38 | 32 |
|
39 | 33 | /**
|
40 | 34 | * Update id from request if param cartId exist
|
41 | 35 | *
|
42 | 36 | * @param GuestCartItemRepositoryInterface $guestCartItemRepository
|
43 | 37 | * @param CartItemInterface $cartItem
|
44 |
| - * @return array |
| 38 | + * @return void |
45 | 39 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
46 | 40 | */
|
47 | 41 | public function beforeSave(
|
48 | 42 | GuestCartItemRepositoryInterface $guestCartItemRepository,
|
49 | 43 | CartItemInterface $cartItem
|
50 |
| - ): array { |
| 44 | + ): void { |
51 | 45 | $cartId = $this->request->getParam('cartId');
|
52 | 46 |
|
53 | 47 | if ($cartId) {
|
54 | 48 | $cartItem->setQuoteId($cartId);
|
55 | 49 | }
|
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 |
| - } |
98 | 50 | }
|
99 | 51 | }
|
0 commit comments