Skip to content

Minimum Qty Allowed in Shopping Cart not working on related product #19566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}