I want to get disabled products for a category in product collection programatically in Magento 2.2.5. I am using following code:
public function __construct(
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->categoryRepository = $categoryRepository;
$this->productRepository = $productRepository;
}
public function deleteProductsByCategoryId($id)
{
$category = $this->categoryRepository->get($id);
$collection = $category->getProductCollection()->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
foreach ($collection as $product) {
// no disabled products are gotten
}
}
But above code does not get disabled products.