0
<?php
try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL;
    exit(1);
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$app->launch();
$objectManager = $bootstrap->getObjectManager();
$productFactory = $objectManager->create('Magento\Catalog\Model\ProductFactory');
$collection = $productFactory->create()->getCollection();
foreach ($collection as $key => $value) {
    echo "<pre/>";print_r($value->getData());exit;
}
echo 'Total products:', $collection->getSize(), PHP_EOL;

This code output like - Total products:0 When i disable foreach it shows me correct product count. Foreach not working with collection even product count is correct.

0

Try this

<?php
try {
    require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL;
    exit(1);
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$app->launch();
$objectManager = $bootstrap->getObjectManager();
$productFactory = $objectManager->create('Magento\Catalog\Model\ProductFactory');
$collection = $productFactory->create()->getCollection();
foreach ($collection as $key => $value) {
    echo "<pre/>";print_r($value->getData());
}
echo 'Total products:', $collection->getSize(), PHP_EOL;

Note: just remove exit;

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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