From 5e6a32eaa051ad0d04a46e1f2fa008615988e565 Mon Sep 17 00:00:00 2001 From: ahmedmwalied Date: Mon, 11 Mar 2024 00:04:22 +0200 Subject: [PATCH] PERF-10: fix add to cart for simple products with options --- src/Model/Quote/Quote.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Model/Quote/Quote.php b/src/Model/Quote/Quote.php index 11d1004..86beb56 100644 --- a/src/Model/Quote/Quote.php +++ b/src/Model/Quote/Quote.php @@ -11,6 +11,10 @@ */ namespace ScandiPWA\QuoteGraphQl\Model\Quote; +use Magento\Catalog\Model\Product; +use Magento\Catalog\Model\Product\Type\AbstractType; +use Magento\Framework\DataObject; +use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Model\Quote as SourceQuote; /** @@ -29,4 +33,36 @@ public function beforeSave() $this->setCustomerIsGuest($customerIsGuest); return $this; } + + public function addProduct( + Product $product, + $request = null, + $processMode = AbstractType::PROCESS_MODE_FULL + ) + { + if ($request === null) { + $request = 1; + } + + if (is_numeric($request)) { + $request = $this->objectFactory->create(['qty' => $request]); + } + + if (!$request instanceof DataObject) { + throw new LocalizedException( + __('We found an invalid request for adding product to quote.') + ); + } + + if (!$product->isSalable()) { + throw new LocalizedException( + __('Product that you are trying to add is not available.') + ); + } + + $productId = $product->getId(); + $product = clone $this->productRepository->getById($productId, false, $this->getStore()->getId()); + + parent::addProduct($product, $request, $processMode); + } }