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