Magento 2.3.2 Collection foreach didn't get disabled product
-
16-03-2021 - |
質問
I am using Magento 2.3.2. I am filtering collection if product ids found in array. before foreach if I am printing collection I am getting data. But in foreach I am not getting any data.
$productIds = array(123,124); // product ids
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToSelect("*")->addFieldToFilter("entity_id", array("in" => $productIds));
echo "<pre>";
echo "#########################################";
print_r($productCollection->getData());
echo "#########################################";
foreach ($productCollection as $product) {
print_r($product->getData());
}
Below values I am getting result of first print
(
[0] => Array
(
[row_id] => 23947
[entity_id] => 23908
[created_in] => 1
[updated_in] => 2147483647
[attribute_set_id] => 22
[type_id] => simple
[sku] => test123
[has_options] => 0
[required_options] => 0
[created_at] => 2018-11-10 17:03:23
[updated_at] => 2019-08-23 06:30:15
[status] => 2
)
[1] => Array
(
[row_id] => 70562
[entity_id] => 69713
[created_in] => 1
[updated_in] => 2147483647
[attribute_set_id] => 19
[type_id] => simple
[sku] => firstprod
[has_options] => 0
[required_options] => 0
[created_at] => 2019-03-21 20:45:31
[updated_at] => 2019-08-25 08:30:50
[status] => 2
)
)
解決
We need to set flag after collection to get disable products from collection object like below
$productCollection = $this->_productFactory->create()->getCollection();
$productCollection->setFlag('has_stock_status_filter', false);
他のヒント
Instead of this
foreach ($productCollection as $product) {
print_r($product->getData();
}
can you pls try once like below ,
foreach ($productCollection as $product) {
echo $product->getId();
}
or if you still want to display data, there is a syntax error in your code,
pls try below code,
foreach ($productCollection as $product) {
print_r($product->getData());
}
所属していません magento.stackexchange