The CSV file format makes it easy to update the data in Magento store.
The admin can quickly import and export data while Magento migration with the use of CSV file format.
Product reviews, customer data, catalogues, products, etc. can be updated instantly with easy import and export.
One may need to update these records in case of changes in the stock or update in the product data.
It can be done easily with the programmatic solution to download all product data in CSV using root script in Magento 2. It is the quick method to get, print, check, and update the required data with the use of object manager in Magento 2.
Method to Download All Product Data in CSV Using Root Script in Magento 2:
- Use the below code in your root script.
<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend');
$productLoader = $objectManager->get('Magento\Catalog\Model\ProductFactory');
$fileFactory = $objectManager->get('Magento\Framework\App\Response\Http\FileFactory');
$productFactory = $objectManager->get('Magento\Catalog\Model\ProductFactory');
$layoutFactory = $objectManager->get('Magento\Framework\View\Result\LayoutFactory');
$csvProcessor = $objectManager->get('Magento\Framework\File\Csv');
$directoryList = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList');
$content[] = ['name' => __('Product Name'),
'entity_id' => __('Entity ID'),
'attribute_set_id' => __('Attribute Set ID'),
'type_id' => __('Type ID'),
'sku' => __('Sku'),
'required_options' => __('Required Options'),
'created_at' => __('Created At'),
'updated_at' => __('Updated At'),];
$product = $productFactory->create()->getCollection();
$collection = $productFactory->create()->getCollection();
$fileName = 'product_excel.xls'; // Add Your CSV File name
$filePath = $directoryList->getPath(DirectoryList::MEDIA) . "/" . $fileName;
while ($product = $collection->fetchItem()) {
$productData = $productLoader->create()->load($product->getEntityId());
$content[] = [$productData->getName(),
$product->getEntityId(),
$product->getAttributeSetId(),
$product->getTypeId(),
$product->getSku(),
$product->getRequiredOptions(),
$product->getCreatedAt(),
$product->getUpdatedAt()];
}
$csvProcessor->setEnclosure('"')->setDelimiter(',')->saveData($filePath, $content);
$fileFactory->create($fileName, ['type' => "filename",
'value' => $fileName,
'rm' => true,
// True => File will be remove from directory after download.
], DirectoryList::MEDIA, 'text/xls', null);
That’s all!
To further manage your product data efficiently, you might also want to import and export product reviews in Magento 2 for effortless review handling.
Please share the solution with the Magento community via social media.
Thank you.