In Magento 2.2.6 from the file
"/app/design/frontend/THEME/Magento_Catalog/templates/product/view/addtocart.phtml"
How can I get the “Qty Increments” that I set as in the screenshot?
I have done a lot of tests but they do not work, for example:
<?php echo $product->getStockItem()->getData('qty_increments') ?>
<?php echo $block->getProductQtyIncrements() ?>
<?php echo $stockItem->getQtyIncrements() ?>
<?php echo $product->getStockItem()->getQtyIncrements() ?>
2
Answers
In order to get the stock status please proceed as follows :
In the addtocart BLock add following in the constructor :
and then initiate it as
then you can create a method like :
and call this from the the template file to get all stock related information of the product.
I suggest to over-ride the block file and then do above suggested changes in it.
Let me know if i was able to help.
This is the solution for Magento 2.2 and above, using the View models approach.
Documentation: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/view-models.html
Create a custom module with the following files
app/code/Vendor/ModuleName/etc/di.xml
app/code/Vendor/ModuleName/etc/module.xml
app/code/Vendor/ModuleName/view/frontend/layout/catalog_product_view.xml
app/code/Vendor/ModuleName/ViewModel/Qty.php
There is no need to create a custom function, since it is enough to extend the “Qtyincrements” Class.
app/code/Vendor/ModuleName/view/frontend/templates/product/view/addtocart.phtml
Now your are able to access the getProductQtyIncrements() via the viewModel like this:
Hope this is useful to future readers. Leave a upvote if it worked for you.