How to get list of products for which backorder is enabled?
1 Answer
Inject this below class in your construct :
protected $collectionFactory;
public function __construct(
.....
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
.....
) {
.....
$this->collectionFactory = $collectionFactory;
.....
}
Now you can use this below code for fetch collection based on backorders.
$productCollection = $this->collectionFactory->create();
$productCollection->addAttributeToSelect('*')->joinField(
'backorders',
'cataloginventory_stock_item',
'backorders',
'product_id = entity_id',
)->joinTable(
'cataloginventory_stock_item',
'product_id = entity_id',
['']
)
->addAttributeToFilter(
'status',
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
)->load();
For backorder enabled only, add more filter :
->addAttributeToFilter(
'backorders',
1
)
-
1