2

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
        )

)
1

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);
| improve this answer | |
0

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());
}
| improve this answer | |
  • This is also not working – Prashant Valanda Oct 14 '19 at 6:35
  • @PrashantValanda, what about, echo $product->getId(); – jafar pinjar Oct 14 '19 at 6:36
  • I am not getting Id as well, because its skip disable products – Prashant Valanda Oct 14 '19 at 6:38
  • @PrashantValanda, then add filter to your collection as like below, ->addAttributeToFilter('status', array('in'=>array(1,2))) – jafar pinjar Oct 14 '19 at 6:40
  • I have also tried that is also not working – Prashant Valanda Oct 14 '19 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.