I am using \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
class to fetch the product collection.
Magento version: 2.2.6
I am trying to fetch both enabled & disabled products with the following code
status > 0, as in Magento, Enabled = 1 and Disabled = 2
$collection = $this->productCollection
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('gt' => 0));
When I print the product collection data:
echo '<pre>';
print_r($collection->getData());
echo '</pre>';
I get the following result:
Array
(
[0] => Array
(
[entity_id] => 27
[attribute_set_id] => 4
[type_id] => virtual
[sku] => my-product-905546
[has_options] => 0
[required_options] => 0
[created_at] => 2018-12-04 10:18:06
[updated_at] => 2018-12-04 10:20:31
)
[1] => Array
(
[entity_id] => 26
[attribute_set_id] => 4
[type_id] => virtual
[sku] => test-123-897069
[has_options] => 0
[required_options] => 0
[created_at] => 2018-12-03 06:38:39
[updated_at] => 2018-12-04 09:57:10
)
)
In the above product result:
- one of the product is disabled and out-of-stock (sku:
my-product-905546
) - the other product is enabled and in-stock (sku:
test-123-897069
)
When I loop through the product collection, it only prints the enabled product name.
foreach ($collection as $product) {
echo $product->getSku();
echo '<br>';
}
Result:
test-123-897069
Expected Result:
my-product-905546
test-123-897069
Why is this so?
Any solution to print the disabled/out-of-stock product inside the foreach loop (load both enabled and disabled products into the collection) please?