3

I'm using Magento 2.3.3 with multiple websites store that uses different multiple stock sources(multi inventory) and I tried to retrieve product collection by category id like this:

public function __construct(
    \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productsCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
){
    $this->_catalogProductVisibility  = $catalogProductVisibility;
    $this->_productCollectionFactory  = $productsCollectionFactory;
    $this->_categoryFactory           = $categoryFactory;
}

$collection = $this->_productCollectionFactory->create();
$category = $this->_categoryFactory->create()->load(3);

$_collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$_collection->addFieldToFilter('status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED));

$_collection = $this->_addProductAttributesAndPrices($_collection)
        ->setCurPage(1);

$_collection->setPageSize($7);

$_collection->addCategoryFilter($category);
$_collection->addAttributeToFilter('is_featured',1);
$_collection->setOrder('created_at','desc');

return $_collection;

The collection will show both in stock and out of stock product collection, how can I filter out the out of stock product and only show the in-stock product collection?

Thanks in advance..!

6
+50

To get instock product collection. include catalogInventory stock helper class

protected $stockFilter;

public function __construct(
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productsCollectionFactory,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\CatalogInventory\Helper\Stock $stockFilter
){
    $this->_catalogProductVisibility  = $catalogProductVisibility;
    $this->_productCollectionFactory  = $productsCollectionFactory;
    $this->_categoryFactory           = $categoryFactory;
    **$this->stockFilter                = $stockFilter;**
}

$collection = $this->_productCollectionFactory->create();
$category = $this->_categoryFactory->create()->load(3);
$_collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());

$_collection->addFieldToFilter('status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED));
$_collection = $this->_addProductAttributesAndPrices($_collection)->setCurPage(1);

$this->stockFilter->addInStockFilterToCollection($_collection);// return instock product collection only
$_collection->setPageSize($7);
$_collection->addCategoryFilter($category);
$_collection->addAttributeToFilter('is_featured',1);
$_collection->setOrder('created_at','desc');

return $_collection;

Need to add addInstockFilterToCollection to get only Instock collection. please accept anser if it is helpful. Thanks

| improve this answer | |

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.