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

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

You need to apply joinField for get only instock items :

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);
  • hi @Rohan, still this filters the collection with out of stock items – jafar pinjar Dec 26 '18 at 6:55
  • Did you clean cache? Check once again. It's working. – Rohan Hapani Dec 26 '18 at 7:07
  • yes cleared cache and checked it. – jafar pinjar Dec 26 '18 at 7:08
  • Check my updated answer. – Rohan Hapani Dec 26 '18 at 7:15
  • can we filter with best seller like order qty? – jafar pinjar Jan 10 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.

  • 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 – jafar pinjar Dec 26 '18 at 7:07
  • For configurable you need to check child product status. – user55548 Dec 26 '18 at 7:17

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.