Please check below Code:
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\Catalog\Block\Product\ListProduct" type="Vendot\Module\Block\Product\ListProduct" />
</config>
Vendor\Module\Block\Product\ListProduct.php
<?php
namespace Vendor\Module\Block\Product;
use Magento\Catalog\Api\CategoryRepositoryInterface;
class ListProduct extends \Magento\Catalog\Block\Product\ListProduct {
protected $_collection;
protected $categoryRepository;
protected $_logger;
private $layerResolver;
protected $_productCollectionFactory;
protected $_categoryCollectionFactory;
protected $_stockFilter;
//protected $_storeManager;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Url\Helper\Data $urlHelper,
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
\Psr\Log\LoggerInterface $logger,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
\Magento\CatalogInventory\Helper\Stock $stockFilter,
array $data = []
) {
$this->categoryRepository = $categoryRepository;
$this->_collection = $collection;
$this->_logger = $logger;
$this->layerResolver = $layerResolver;
$this->_productCollectionFactory = $productCollectionFactory;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_stockFilter = $stockFilter;
parent::__construct($context, $postDataHelper, $layerResolver,$categoryRepository, $urlHelper, $data);
}
public function getProducts() {
$currentCategory = $this->layerResolver->get()->getCurrentCategory();
$category_id = $currentCategory->getId();
//$collection = $this->_productCollectionFactory->create();
$layer = $this->layerResolver->get();
if(!$category_id) {
$category_id = $this->_storeManager->getStore()->getRootCategoryId();
}
$category = $this->categoryRepository->get($category_id);
$layer->setCurrentCategory($category);
$collection = $layer->getProductCollection();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
$collection->addAttributeToSort('created_at','desc');
$this->_stockFilter->addInStockFilterToCollection($collection); // Getting Error
$collection->joinField(
'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
); // Getting Error
$collection->addAttributeToFilter('qty',['gt'=>0]) ->load(); // Getting Error
return $collection;
}
public function getLoadedProductCollection() {
return $this->getProducts();
}
}
Below code Throwing the Error with Category Layer Collection but with
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
It's working fine.
$this->_stockFilter->addInStockFilterToCollection($collection); // Getting Error
$collection->joinField(
'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
); // Getting Error
$collection->addAttributeToFilter('qty',['gt'=>0]) ->load(); // Getting Error
Here Backend Magento Catalog Out Of Stock Products Display Settings are Enabled.
If Anyone having any idea please suggest me. Thanks in Advance..!!