0

I need to filter category product collection with Instock items and only simple products.

I have used below code to implement it.But the out of stock products also fetching with the collection.

class CategoryData extends \Magento\Framework\View\Element\Template
{  
    protected $categoryFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    ) { 
        $this->categoryFactory = $categoryFactory;
        parent::__construct($context);
    }
    
    public function getSimilarCollection($productId)
    {
        $categoryId = 10;
        $category = $this->categoryFactory->create()->load($categoryId); 
        $categoryProducts = $category->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
            ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)    
            ->setPageSize(6);
        return $categoryProducts;
    }
}

Can anyone suggest me how to filter the above collection with instock items. Thanks

3
  • Try to add code before return statement $categoryProducts->joinField( 'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left' );
    – user55548
    Commented Dec 26, 2018 at 6:55
  • @user55548, can you pls update to my question Commented Dec 26, 2018 at 6:56
  • I have added complete code in answer section.
    – user55548
    Commented Dec 26, 2018 at 7:03

2 Answers 2

2

You need to apply joinField to get instock items only:

Method 1 :

$category = $this->categoryFactory->create()->load($categoryId); 
$categoryProducts = $category->getProductCollection()
                         ->addAttributeToSelect('*')
                         ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
                         ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                         ->addAttributeToFilter('type_id',\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
                         ->joinField('stock_item', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id', 'is_in_stock=1')
                         ->setPageSize(6);

Method 2 :

$category = $this->categoryFactory->create()->load($categoryId); 
$categoryProducts = $category->getProductCollection()
                         ->addAttributeToSelect('*')
                         ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
                         ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                         ->addAttributeToFilter('type_id',\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)    
                         ->addAttributeToFilter('is_in_stock', 1)
                         ->setPageSize(6);
5
  • hi @Rohan, still this filters the collection with out of stock items Commented Dec 26, 2018 at 6:55
  • Did you clean cache? Check once again. It's working. Commented Dec 26, 2018 at 7:07
  • yes cleared cache and checked it. Commented Dec 26, 2018 at 7:08
  • Check my updated answer. Commented Dec 26, 2018 at 7:15
  • can we filter with best seller like order qty? Commented Jan 10, 2019 at 15:21
1

Here is complete code.

$categoryId = 10;
$category = $this->categoryFactory->create()->load($categoryId); 
$categoryProducts = $category->getProductCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)                                
                    ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                    ->setPageSize(6);
$categoryProducts->joinField( 'qty', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left' );

return $categoryProducts;

Please let me know if it any issue.

2
  • hi, i can still see the collection is fetching the out of stock item after clearing cache also. that product is configurable product. so, can we apply other filter to filter only simple products Commented Dec 26, 2018 at 7:07
  • For configurable you need to check child product status.
    – user55548
    Commented Dec 26, 2018 at 7:17

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.