Disable URL redirection from catalog to product view in case of the error “The requested qty is not available” when adding too much products to cart

magento.stackexchange https://magento.stackexchange.com/questions/302657

  •  06-04-2021
  •  | 
  •  

Question

I can't figure where to find the code displaying "The requested qty is not available" and redirecting to the product URL when we add more product than the stock.

[I'm using Magento 2.3.3 and custom theme and module]

I want to do TWO things :

  • Remove the href redirecting from the catalog to the product page

  • Display "The requested qty is not available" in catalog view when we add more product than the stock

When i do this (i add 61 items and the stock left is 60) :

I am redirecting to product page (and i don't want to), and i would move the error message to catalog view :

Thanks in advance for your help !

Alex

Was it helpful?

Solution

I've finally found a solution which solve my problem, i override app/design/frontend/package/theme/Magento_Catalog/templates/product/list.phtml in my custom theme, and add a MAX parameter to the input box

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$stockQty =  $StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());
?>

<input type="number" min="1" max="<?= $stockQty ?>" step="1" name="qty" ng-attr-id="{{ 'Quantity-' + myScopeObject.index }}"
style="width: 110px" maxlength="12"
value="<?php /* @escapeNotVerified */ echo '0' ?>"
title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty" />
</div>

which is solving my problem

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top