2

i am trying to filter inStock product in Magento 2 Using I am trying to filter inStock product in Magento 2 Using

\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,

by

$collection = $this->_collectionFactory->create();
        $collection->addAttributeToSelect('*');
        //$collection->addAttributeToFilter('rand()');
        //$collection->addAttributeToFilter('flavor',$flavorId);
        $collection->addCategoriesFilter(['in' => $searchCategory]);
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
        $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
        $collection->getSelect()->orderRand();
        $collection->setPageSize(3);
        $collection->getSelect()->orderRand();
        $collection->joinField(
        'qty', 'csvp_cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=2', 'left'
            );
        //$collection->joinField('stock_item', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty=0');
        return $collection;

but the result is also shown out of stock products. can any one suggest me how to get only in stock products collection.

4 Answers 4

7

You can also try below code

$pCollection = $this->getObjectManager()->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
        $pCollection->setStoreId($storeId);
        $pCollection->addAttributeToSelect('*');
        $pCollection->addFieldToFilter('visibility', array('neq' => \Magento\Catalog\Model\Product\Visibility::VISIBILITY_NOT_VISIBLE));
        $pCollection->addFieldToFilter('status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED));
$pCollection->joinField(
    'stock_status', 'cataloginventory_stock_status', 'stock_status', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
)->addFieldToFilter('stock_status', array('eq' => \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK));

Thanks

2

You can use this class \Magento\CatalogInventory\Helper\Stock

 $stockFilter->addInStockFilterToCollection($collection);
1
  • This one causes strange issues. It adds an inner join with AND ( (at_inventory_in_stock.use_config_manage_stock = 0 AND at_inventory_in_stock.manage_stock=1 AND at_inventory_in_stock.is_in_stock=1) OR (at_inventory_in_stock.use_config_manage_stock = 0 AND at_inventory_in_stock.manage_stock=0) OR (at_inventory_in_stock.use_config_manage_stock = 1 AND at_inventory_in_stock.is_in_stock=1) )
    – medina
    Commented Jul 14, 2020 at 4:42
1

Please try this

$collection = $this->_collectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addCategoriesFilter(['in' => $searchCategory]);
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
        $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
        $collection->getSelect()->orderRand();
        $collection->setPageSize(3);
        $collection->getSelect()->orderRand();
        $collection->joinField(
        'qty', 'csvp_cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=2', 'left'
            );

        $collection->getSelect()->where('csvp_stock_status_index.stock_status = ?', 1);

        return $collection;
1

Try this,

$collection = $this->_collectionFactory->create();
        $collection->addAttributeToSelect('*');
        //$collection->addAttributeToFilter('rand()');
        //$collection->addAttributeToFilter('flavor',$flavorId);
        $collection->addCategoriesFilter(['in' => $searchCategory]);
        $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()]);
        $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
        $collection->getSelect()->orderRand();
        $collection->setPageSize(3);
        $collection->getSelect()->orderRand();

        $collection->joinField('stock_item', 'csvp_cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty=0');
        ->addAttributeToSelect('stock_status')
        ->addFieldToFilter('stock_status', ['eq' => 1])
        return $collection;
1
  • How is the "addFieldToFilter()"-Method handling the conditions? When I am using it (for 'more products of brand'), it does not seem to change anything in the output. When I use "inner"-join and extend the condition there, it does. Commented May 2, 2023 at 9:07

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.