From 1cdca1b57f3810fd5b88d548b3ba12a86e5b95b8 Mon Sep 17 00:00:00 2001 From: Artjoms Travkovs Date: Thu, 31 Oct 2019 21:29:54 +0200 Subject: [PATCH 1/3] Added stock quantity check --- src/Model/Resolver/SaveCartItem.php | 39 +++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Model/Resolver/SaveCartItem.php b/src/Model/Resolver/SaveCartItem.php index d6ae014..86bfbea 100644 --- a/src/Model/Resolver/SaveCartItem.php +++ b/src/Model/Resolver/SaveCartItem.php @@ -36,6 +36,8 @@ use Magento\Bundle\Model\Product\Type; use Magento\Framework\DataObject; use Magento\Catalog\Model\Product\Attribute\Repository; +use Magento\CatalogInventory\Api\StockStatusRepositoryInterface; +use Magento\Quote\Api\Data\CartItemInterface; /** * Class SaveCartItem @@ -73,6 +75,16 @@ class SaveCartItem implements ResolverInterface */ protected $quoteIdMaskResource; + /** + * @var Configurable + */ + protected $configurableType; + + /** + * @var StockStatusRepositoryInterface + */ + protected $stockStatusRepository; + /** * SaveCartItem constructor. * @param QuoteIdMaskFactory $quoteIdMaskFactory @@ -88,7 +100,9 @@ public function __construct( ParamOverriderCartId $overriderCartId, ProductRepository $productRepository, Repository $attributeRepository, - QuoteIdMask $quoteIdMaskResource + QuoteIdMask $quoteIdMaskResource, + Configurable $configurableType, + StockStatusRepositoryInterface $stockStatusRepository ) { $this->quoteIdMaskFactory = $quoteIdMaskFactory; @@ -97,6 +111,8 @@ public function __construct( $this->productRepository = $productRepository; $this->attributeRepository = $attributeRepository; $this->quoteIdMaskResource = $quoteIdMaskResource; + $this->configurableType = $configurableType; + $this->stockStatusRepository = $stockStatusRepository; } /** @@ -228,6 +244,8 @@ public function resolve( $itemId = $this->getItemId($requestCartItem); if ($itemId) { $cartItem = $quote->getItemById($itemId); + $this->checkItemQty($cartItem, $qty); + $cartItem->setQty($qty); $this->quoteRepository->save($quote); } else { @@ -253,7 +271,24 @@ public function resolve( return []; } - + + protected function checkItemQty(CartItemInterface $cartItem, $qty): void + { + $product = $cartItem->getProduct(); + + if ($cartItem->getProductType() === Configurable::TYPE_CODE) { + $attributesInfo = $cartItem->getBuyRequest()->getDataByKey('super_attribute'); + $product = $this->configurableType->getProductByAttributes($attributesInfo, $product); + } + + $stockStatus = $this->stockStatusRepository->get($product->getId()); + $stockItem = $stockStatus->getStockItem(); + + if ($qty < $stockItem->getMinSaleQty() || $qty > $stockItem->getMaxSaleQty()) { + throw new GraphQlInputException(new Phrase('Provided quantity exceeds stock limits')); + } + } + /** * @param string $sku * @param int $qty From 327fb54d7f31e91cda71313bdfef3b86a69bb03b Mon Sep 17 00:00:00 2001 From: Artjoms Travkovs Date: Thu, 14 Nov 2019 21:50:04 +0200 Subject: [PATCH 2/3] Added check for max stock qty --- src/Model/Resolver/SaveCartItem.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Model/Resolver/SaveCartItem.php b/src/Model/Resolver/SaveCartItem.php index 86bfbea..28b28d9 100644 --- a/src/Model/Resolver/SaveCartItem.php +++ b/src/Model/Resolver/SaveCartItem.php @@ -284,7 +284,10 @@ protected function checkItemQty(CartItemInterface $cartItem, $qty): void $stockStatus = $this->stockStatusRepository->get($product->getId()); $stockItem = $stockStatus->getStockItem(); - if ($qty < $stockItem->getMinSaleQty() || $qty > $stockItem->getMaxSaleQty()) { + $fitsInStock = $qty <= $stockItem->getQty(); + $isInMinMaxSaleRange = $qty >= $stockItem->getMinSaleQty() || $qty <= $stockItem->getMaxSaleQty(); + + if (!($fitsInStock && $isInMinMaxSaleRange)) { throw new GraphQlInputException(new Phrase('Provided quantity exceeds stock limits')); } } From afc54f8c4d346a25e2d1500d0c9ed09d2da203ea Mon Sep 17 00:00:00 2001 From: Artjoms Travkovs Date: Fri, 15 Nov 2019 20:40:19 +0200 Subject: [PATCH 3/3] Enhanced qty excceeded error handling --- src/Model/Resolver/SaveCartItem.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Model/Resolver/SaveCartItem.php b/src/Model/Resolver/SaveCartItem.php index 28b28d9..21d7563 100644 --- a/src/Model/Resolver/SaveCartItem.php +++ b/src/Model/Resolver/SaveCartItem.php @@ -256,13 +256,18 @@ public function resolve( } $newQuoteItem = $this->buildQuoteItem($sku, $qty, (int)$quoteId, $requestCartItem['product_option'] ?? []); - - $quote->addProduct($product, $this->prepareAddItem( - $product, - $newQuoteItem - )); - $this->quoteRepository->save($quote); - + + try { + $quote->addProduct($product, $this->prepareAddItem( + $product, + $newQuoteItem + )); + + $this->quoteRepository->save($quote); + } catch (\Exception $e) { + throw new GraphQlInputException(new Phrase($e->getMessage())); + } + // Related to bug: https://github.com/magento/magento2/issues/2991 $quote = $this->quoteRepository->getActive($quoteId); $quote->setTotalsCollectedFlag(false)->collectTotals();