Skip to content

Commit

Permalink
ENGCOM-3892: Minimum Qty Allowed in Shopping Cart not working on rela…
Browse files Browse the repository at this point in the history
…ted product #19566
  • Loading branch information
sivaschenko authored Feb 18, 2019
2 parents 23c7839 + 7d2f143 commit 3f8dee5
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions app/code/Magento/Checkout/Model/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Shopping cart model
*
* @api
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @deprecated 100.1.0 Use \Magento\Quote\Model\Quote instead
* @see \Magento\Quote\Api\Data\CartInterface
Expand Down Expand Up @@ -365,20 +366,10 @@ protected function _getProductRequest($requestInfo)
public function addProduct($productInfo, $requestInfo = null)
{
$product = $this->_getProduct($productInfo);
$request = $this->_getProductRequest($requestInfo);
$productId = $product->getId();

if ($productId) {
$stockItem = $this->stockRegistry->getStockItem($productId, $product->getStore()->getWebsiteId());
$minimumQty = $stockItem->getMinSaleQty();
//If product quantity is not specified in request and there is set minimal qty for it
if ($minimumQty
&& $minimumQty > 0
&& !$request->getQty()
) {
$request->setQty($minimumQty);
}

$request = $this->getQtyRequest($product, $requestInfo);
try {
$this->_eventManager->dispatch(
'checkout_cart_product_add_before',
Expand Down Expand Up @@ -438,8 +429,9 @@ public function addProductsByIds($productIds)
}
$product = $this->_getProduct($productId);
if ($product->getId() && $product->isVisibleInCatalog()) {
$request = $this->getQtyRequest($product);
try {
$this->getQuote()->addProduct($product);
$this->getQuote()->addProduct($product, $request);
} catch (\Exception $e) {
$allAdded = false;
}
Expand Down Expand Up @@ -762,4 +754,27 @@ private function getRequestInfoFilter()
}
return $this->requestInfoFilter;
}

/**
* Get request quantity
*
* @param Product $product
* @param \Magento\Framework\DataObject|int|array $request
* @return int|DataObject
*/
private function getQtyRequest($product, $request = 0)
{
$request = $this->_getProductRequest($request);
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
$minimumQty = $stockItem->getMinSaleQty();
//If product quantity is not specified in request and there is set minimal qty for it
if ($minimumQty
&& $minimumQty > 0
&& !$request->getQty()
) {
$request->setQty($minimumQty);
}

return $request;
}
}

0 comments on commit 3f8dee5

Please sign in to comment.