0

I have used below code to override helper class.

Vendor/Module/etc/di.xml

 <?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Magento\CatalogInventory\Helper\Stock" type="Vendor\Module\Helper\Stock" />
</config>

Then Vendor\Module\Helper\Stock.php

<?php   
namespace Vendor\Module\Helper;  
class Stock extends \Magento\CatalogInventory\Helper\Stock
{

public function addIsInStockFilterToCollection($collection)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')
                              ->registry('current_category');//get current category
     $categoryId = $category->getId(); 
     $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testlog.log');
     $logger = new \Zend\Log\Logger();
     $logger->addWriter($writer);       
    $logger->info('Category Id: '.$categoryId); 
    $parentCategoryId =  $category->getParentCategory()->getId();
    if(!is_null($category))
    {
        $stockFlag = 'has_stock_status_filter';

        if (!$collection->hasFlag($stockFlag)) {
            $isShowOutOfStock = $this->scopeConfig->getValue(
                \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            );
            $resource = $this->getStockStatusResource();
            /* $resource->addStockDataToCollection(
                $collection,
                !$isShowOutOfStock
            ); */
            if($categoryId == 108 || $parentCategoryId == 108)
            {
               //show display out of stock products for particular category
               $resource->addStockDataToCollection(
                    $collection,
                    false
                );
            }else{
                $resource->addStockDataToCollection(
                    $collection,
                    $isShowOutOfStock
                );
            }
            $collection->setFlag($stockFlag, true);
        }

      }
   }
}

This is not overriding the mentioned helper class, Can anyone help me where i am wrong in the above code please. Thanks!!

3
  • Have you try bt clear the cache and deploy command again? Feb 7, 2020 at 6:48
  • @DhirenVasoya, yes still its same not overrided Feb 7, 2020 at 6:51
  • Paste all module files... Maybe your module is not well registered? Feb 7, 2020 at 6:54

2 Answers 2

1

I think the code you have highlighted looks looks OK. Make sure that your module.xml file is declaring Magento_CatalogInventory as a dependancy.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" >
        <sequence>
            <module name="Magento_CatalogInventory"/>
        </sequence>
    </module>
</config>

If you hadn't already done this you may need to run magento setup:upgrade --keep-generated to update the module sequence in config.php. Also make sure you have run bin/magento setup:di:compile or deleted the generated directory.

1
  • This was the issue, Now the file is overriding. Feb 7, 2020 at 7:11
0

app/code/Vendor/Module/etc/di.xml

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

    <preference for="Magento\CatalogInventory\Helper\Stock" type="Vendor\Module\Helper\Magento\CatalogInventory\Stock" />

</config>

app/code/Vendor/Module/Helper/Magento/CatalogInventory/Stock.php

<?php

namespace Vendor\Module\Helper\Magento\CatalogInventory;


class Stock extends \Magento\CatalogInventory\Helper\Stock
{
    public function addIsInStockFilterToCollection($collection)
    {
        // your code
    }

}

I have added only di.xml and Helper class file code. I assume other files which is necessary for module is created properly.

Please let me know still if you have any issue.

2
  • hi, yes i have also used same thing here, u just added some other directories Feb 7, 2020 at 7:05
  • In your function addIsInStockFilterToCollection remove all code and just add echo "hello"; die; and check your helper file is called or not
    – user55548
    Feb 7, 2020 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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