/** * Update item in shopping cart (quote) * $requestInfo - either qty (int) or buyRequest in form of array or \Magento\Framework\Object * $updatingParams - information on how to perform update, passed to Quote->updateItem() method * * @param int $itemId * @param int|array|\Magento\Framework\Object $requestInfo * @param null|array|\Magento\Framework\Object $updatingParams * @return \Magento\Sales\Model\Quote\Item|string * @throws \Magento\Framework\Model\Exception * * @see \Magento\Sales\Model\Quote::updateItem() */ public function updateItem($itemId, $requestInfo = null, $updatingParams = null) { try { $item = $this->getQuote()->getItemById($itemId); if (!$item) { throw new \Magento\Framework\Model\Exception(__('This quote item does not exist.')); } $productId = $item->getProduct()->getId(); $product = $this->_getProduct($productId); $request = $this->_getProductRequest($requestInfo); if ($productId) { $minimumQty = $this->stockItemService->getMinSaleQty($productId); // If product was not found in cart and there is set minimal qty for it if ($minimumQty && $minimumQty > 0 && $request->getQty() < $minimumQty && !$this->getQuote()->hasProductId($productId)) { $request->setQty($minimumQty); } } $result = $this->getQuote()->updateItem($itemId, $request, $updatingParams); } catch (\Magento\Framework\Model\Exception $e) { $this->_checkoutSession->setUseNotice(false); $result = $e->getMessage(); } /** * We can get string if updating process had some errors */ if (is_string($result)) { if ($this->_checkoutSession->getUseNotice() === null) { $this->_checkoutSession->setUseNotice(true); } throw new \Magento\Framework\Model\Exception($result); } $this->_eventManager->dispatch('checkout_cart_product_update_after', array('quote_item' => $result, 'product' => $product)); $this->_checkoutSession->setLastAddedProductId($productId); return $result; }
public function testGetMinSaleQty() { $productId = 51; $result = 2; $stockItemModel = $this->getStockItemModel($productId); $stockItemModel->expects($this->once())->method('getMinSaleQty')->will($this->returnValue($result)); $this->assertEquals($result, $this->model->getMinSaleQty($productId)); }
/** * Gets minimal sales quantity * * @param \Magento\Catalog\Model\Product $product * @return int|null */ public function getMinimalQty($product) { $minSaleQty = $this->stockItemService->getMinSaleQty($product->getId()); return $minSaleQty > 0 ? $minSaleQty : null; }