skip to Main Content

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() ?>

Screenshot Qty Increments

2

Answers


  1. In order to get the stock status please proceed as follows :

    In the addtocart BLock add following in the constructor :

    MagentoCatalogInventoryModelStockStockItemRepository $stockItemRepository,
    

    and then initiate it as

    $this->_stockItemRepository = $stockItemRepository;
    

    then you can create a method like :

    public function getStockItem($productId)
    {
        return $this->_stockItemRepository->get($productId);
    }
    

    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.

    Login or Signup to reply.
  2. 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

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
        <type name="VendorModuleNameViewModelQty" />
    </config>
    

    app/code/Vendor/ModuleName/etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Vendor_ModuleName" setup_version="1.0.0" />
    </config>
    

    app/code/Vendor/ModuleName/view/frontend/layout/catalog_product_view.xml

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="product.info.addtocart.additional" template="Vendor_ModuleName::product/view/addtocart.phtml">
                <arguments>
                    <argument name="view_model" xsi:type="object">VendorModuleNameViewModelQty</argument>
                </arguments>
            </referenceBlock>
            <referenceBlock name="product.info.addtocart" template="Vendor_ModuleName::product/view/addtocart.phtml">
                <arguments>
                    <argument name="view_model" xsi:type="object">VendorModuleNameViewModelQty</argument>
                </arguments>
            </referenceBlock>
        </body>
    </page>
    

    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.

    <?php
    namespace VendorModuleNameViewModel;
    
    use MagentoFrameworkViewElementBlockArgumentInterface;
    use MagentoCatalogInventoryBlockQtyincrements;
    
    class Qty extends Qtyincrements implements ArgumentInterface {}
    

    app/code/Vendor/ModuleName/view/frontend/templates/product/view/addtocart.phtml

    Now your are able to access the getProductQtyIncrements() via the viewModel like this:

    ...
    <?php $viewModel = $block->getViewModel(); ?>
    <?php $productQtyIncrements = $viewModel->getProductQtyIncrements(); ?>
    ...
    

    Hope this is useful to future readers. Leave a upvote if it worked for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search