Create a gist now

Instantly share code, notes, and snippets.

Magento 2 - get product collection count via runtime
<?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();
echo 'Total products:', $collection->count(), PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment