88
99namespace Magento \Quote \Plugin ;
1010
11- use Magento \Catalog \Api \ProductRepositoryInterface ;
1211use Magento \Framework \Exception \LocalizedException ;
1312use Magento \Framework \Exception \NoSuchEntityException ;
1413use Magento \Framework \Webapi \Rest \Request as RestRequest ;
@@ -27,14 +26,14 @@ class UpdateCartId
2726{
2827 /**
2928 * @param RestRequest $request
30- * @param ProductRepositoryInterface $productRepository
3129 * @param StoreManagerInterface $storeManager
3230 * @param QuoteIdMaskFactory $quoteIdMaskFactory
3331 * @param CartRepositoryInterface $cartRepository
32+ * @param ProductResource $productResource
33+ * @param ProductWebsiteLink $productWebsiteLink
3434 */
3535 public function __construct (
3636 private readonly RestRequest $ request ,
37- private readonly ProductRepositoryInterface $ productRepository ,
3837 private readonly StoreManagerInterface $ storeManager ,
3938 private readonly QuoteIdMaskFactory $ quoteIdMaskFactory ,
4039 private readonly CartRepositoryInterface $ cartRepository ,
@@ -78,22 +77,33 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
7877 return ;
7978 }
8079
81- $ maskedQuoteId = $ cartItem ->getQuoteId ();
82- $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ maskedQuoteId , 'masked_id ' );
83- $ quoteId = $ quoteIdMask ->getQuoteId ();
80+ $ storeId = (int )($ cartItem ->getStoreId () ?? 0 );
8481
85- if (!$ quoteId ) {
86- return ;
82+ if (!$ storeId ) {
83+ try {
84+ $ storeId = (int )$ this ->storeManager ->getStore ()->getId ();
85+ } catch (\Throwable $ e ) {
86+ // ignore; fallback to masked quote resolution below
87+ }
8788 }
8889
89- try {
90- $ quote = $ this ->cartRepository ->get ($ quoteId );
91- $ storeId = $ quote ->getStoreId ();
92- // Product not in quote yet
93- $ this ->validateWebsiteAssignmentBySku ($ sku , $ storeId );
90+ if (!$ storeId ) {
91+ try {
92+ $ maskedQuoteId = $ cartItem ->getQuoteId ();
93+ if ($ maskedQuoteId ) {
94+ $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ maskedQuoteId , 'masked_id ' );
95+ $ quoteId = (int )$ quoteIdMask ->getQuoteId ();
96+ if ($ quoteId ) {
97+ $ storeId = (int )$ this ->cartRepository ->get ($ quoteId )->getStoreId ();
98+ }
99+ }
100+ } catch (NoSuchEntityException ) {
101+ throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
102+ }
103+ }
94104
95- } catch ( NoSuchEntityException ) {
96- throw new LocalizedException ( __ ( ' Product that you are trying to add is not available. ' ) );
105+ if ( $ storeId ) {
106+ $ this -> validateWebsiteAssignmentBySku ( $ sku , $ storeId );
97107 }
98108 }
99109
0 commit comments