0

I wish the qty valyue could be dynamic (I mean, get another value if product qty had increment) so I create a module and put a helper. Data.php

 
  namespace Vendor\Module\Helper;
 
  class Data extends \Magento\Framework\App\Helper\AbstractHelper
  {
     public function __construct(\Magento\CatalogInventory\Block\Qtyincrements $qtyIncrements) {
          $this->_qtyIncrements = $qtyIncrements;
      }
 
      public function getProductQtyIncrements()
      {
          return $this->_qtyIncrements->getProductQtyIncrements();
      }
  }```
 
and I call this method at app/design/frontend/Smartwave/porto_child/Magento_Catalog/templates/product/view/addtocart.phtml, it's work very well, but I need to do the same thing on magento_catalog/templates/product/list.phtml, but it return a error: 

```<input type="text" name="qty" id="qty" maxlength="12" value="Error: Call to a member function getId() on null in /home/master/autopec/vendor/magento/module-catalog-inventory/Block/Qtyincrements.php:89
Stack trace:
#0 /home/master/autopec/app/code/Vendor/Module/Helper/Data.php(13): Magento\CatalogInventory\Block\Qtyincrements->getProductQtyIncrements()
#1 /home/master/autopec/app/design/frontend/Smartwave/porto_child/Magento_Catalog/templates/product/list.phtml(282): Vendor\Module\Helper\Data->getProductQtyIncrements()
#2 /home/master/autopec/vendor/magento/framework/View/TemplateEngine/Php.php(71): include('/home/master/au...')```

Could someone help call method in this part of module please ?
2

1 Answer 1

0

You cannot use this block on product listing and it's not a good idea to use Product Page block in helper

If you want to use this logic in helper, you need to do something like:

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Helper;

use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Stock extends AbstractHelper
{
    protected StockRegistryInterface $stockRegistry;

    public function __construct(
        Context $context,
        StockRegistryInterface $stockRegistry
    ) {
        parent::__construct($context);
        $this->stockRegistry = $stockRegistry;
    }

    public function getProductQtyIncrements(Product $product)
    {
        $stockItem = $this->stockRegistry->getStockItem(
            $product->getId(),
            $product->getStore()->getWebsiteId()
        );

        $value = $stockItem->getQtyIncrements();
        if (!$product->isSaleable()) {
            $value = false;
        }

        return $value;
    }
}

And pass product object to getProductQtyIncrements method. Example

$stockHelper = $this->helper('Acme\StackExchange\Helper\Stock');
// ...
foreach ($_productCollection as $_product):
// ...
$qtyInc = $stockHelper->getProductQtyIncrements($_product);
// ...
1
  • thank you so much, this really work for me :)
    – Flavia
    18 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.