Having trouble figuring out how to filter a product collection by in stock / out of stock status. My goal is to get all out of stock products that also match some other filters:
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}
$product = $this->collectionFactory->create()
->addAttributeToSelect('*')
->addFieldToFilter('quantity_and_stock_status', array('eq' => 0))
->addFieldToFilter('supply_discontinued', array('eq' => 1))
->addFieldToFilter('status', 1)
->addFieldToFilter('visibility', array('in' => array(3,4)))
->setPageSize(1)
->getFirstItem();
Filtering by quantity_and_stock_status
doesn't seem to work in this instance. Is there another way to filter by stock status as well as the other filters I have listed?