0

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..!!

  • you can try join query to ->joinField('stock_item', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty>1') It's filtered by qty>1. If you want to filter by is_in_stock=1, change the join field to ->joinField('stock_item', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id', 'is_in_stock=1'). To be clear, use only one of the filters (qty>1 OR is_in_stock=1), not both. – Sangeeta Chandaliya Oct 4 at 10:47
  • @sangeeta ->joinField('stock_item', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty=0') query throwing the error while layer Products collections – Hitesh Balpande Oct 4 at 10:49
  • @sangeeta this error i am getting after adding your suggested code 1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): A joined field with this alias is already declared. – Hitesh Balpande Oct 4 at 10:52
  • $collection->addAttributeToSelect ( '*' )->setFlag('has_stock_status_filter', true); $collectionss = $collection->joinField('qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1','left')->joinTable('cataloginventory_stock_item', 'product_id=entity_id', array('stock_status' => 'is_in_stock')) ->addAttributeToSelect('stock_status')->addAttributeToSort ( 'entity_id', 'DESC' )->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)->load(); return $collectionss; – Sangeeta Chandaliya Oct 4 at 11:09
  • @sangeeta sorry your code not working with $layer->getProductCollection(); but same code is working with \Magento\Catalog\Model\ResourceModel\Category\CollectionFactoryas i mentioned in my question. – Hitesh Balpande Oct 4 at 11:18

Your Answer

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

Browse other questions tagged or ask your own question.