Skip to content

Commit

Permalink
Merge pull request #16 from atravkovs/stock-quantity-check
Browse files Browse the repository at this point in the history
Added stock quantity check
  • Loading branch information
alfredsgenkins authored Dec 18, 2019
2 parents 991caff + afc54f8 commit 1cb9393
Showing 1 changed file with 52 additions and 9 deletions.
61 changes: 52 additions & 9 deletions src/Model/Resolver/SaveCartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,6 +75,16 @@ class SaveCartItem implements ResolverInterface
*/
protected $quoteIdMaskResource;

/**
* @var Configurable
*/
protected $configurableType;

/**
* @var StockStatusRepositoryInterface
*/
protected $stockStatusRepository;

/**
* SaveCartItem constructor.
* @param QuoteIdMaskFactory $quoteIdMaskFactory
Expand All @@ -88,7 +100,9 @@ public function __construct(
ParamOverriderCartId $overriderCartId,
ProductRepository $productRepository,
Repository $attributeRepository,
QuoteIdMask $quoteIdMaskResource
QuoteIdMask $quoteIdMaskResource,
Configurable $configurableType,
StockStatusRepositoryInterface $stockStatusRepository
)
{
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
Expand All @@ -97,6 +111,8 @@ public function __construct(
$this->productRepository = $productRepository;
$this->attributeRepository = $attributeRepository;
$this->quoteIdMaskResource = $quoteIdMaskResource;
$this->configurableType = $configurableType;
$this->stockStatusRepository = $stockStatusRepository;
}

/**
Expand Down Expand Up @@ -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 {
Expand All @@ -238,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();
Expand All @@ -253,7 +276,27 @@ 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();

$fitsInStock = $qty <= $stockItem->getQty();
$isInMinMaxSaleRange = $qty >= $stockItem->getMinSaleQty() || $qty <= $stockItem->getMaxSaleQty();

if (!($fitsInStock && $isInMinMaxSaleRange)) {
throw new GraphQlInputException(new Phrase('Provided quantity exceeds stock limits'));
}
}

/**
* @param string $sku
* @param int $qty
Expand Down

0 comments on commit 1cb9393

Please sign in to comment.