Skip to content

Commit 3f8dee5

Browse files
authored
ENGCOM-3892: Minimum Qty Allowed in Shopping Cart not working on related product #19566
2 parents 23c7839 + 7d2f143 commit 3f8dee5

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

app/code/Magento/Checkout/Model/Cart.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Shopping cart model
1717
*
1818
* @api
19+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1920
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2021
* @deprecated 100.1.0 Use \Magento\Quote\Model\Quote instead
2122
* @see \Magento\Quote\Api\Data\CartInterface
@@ -365,20 +366,10 @@ protected function _getProductRequest($requestInfo)
365366
public function addProduct($productInfo, $requestInfo = null)
366367
{
367368
$product = $this->_getProduct($productInfo);
368-
$request = $this->_getProductRequest($requestInfo);
369369
$productId = $product->getId();
370370

371371
if ($productId) {
372-
$stockItem = $this->stockRegistry->getStockItem($productId, $product->getStore()->getWebsiteId());
373-
$minimumQty = $stockItem->getMinSaleQty();
374-
//If product quantity is not specified in request and there is set minimal qty for it
375-
if ($minimumQty
376-
&& $minimumQty > 0
377-
&& !$request->getQty()
378-
) {
379-
$request->setQty($minimumQty);
380-
}
381-
372+
$request = $this->getQtyRequest($product, $requestInfo);
382373
try {
383374
$this->_eventManager->dispatch(
384375
'checkout_cart_product_add_before',
@@ -438,8 +429,9 @@ public function addProductsByIds($productIds)
438429
}
439430
$product = $this->_getProduct($productId);
440431
if ($product->getId() && $product->isVisibleInCatalog()) {
432+
$request = $this->getQtyRequest($product);
441433
try {
442-
$this->getQuote()->addProduct($product);
434+
$this->getQuote()->addProduct($product, $request);
443435
} catch (\Exception $e) {
444436
$allAdded = false;
445437
}
@@ -762,4 +754,27 @@ private function getRequestInfoFilter()
762754
}
763755
return $this->requestInfoFilter;
764756
}
757+
758+
/**
759+
* Get request quantity
760+
*
761+
* @param Product $product
762+
* @param \Magento\Framework\DataObject|int|array $request
763+
* @return int|DataObject
764+
*/
765+
private function getQtyRequest($product, $request = 0)
766+
{
767+
$request = $this->_getProductRequest($request);
768+
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
769+
$minimumQty = $stockItem->getMinSaleQty();
770+
//If product quantity is not specified in request and there is set minimal qty for it
771+
if ($minimumQty
772+
&& $minimumQty > 0
773+
&& !$request->getQty()
774+
) {
775+
$request->setQty($minimumQty);
776+
}
777+
778+
return $request;
779+
}
765780
}

0 commit comments

Comments
 (0)