0

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 result is also showing out of stock products. can any one suggest me how to get only instock products collection.

0

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;
0

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;
0

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

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.