How to Download All Product Data in CSV Using Root Script in Magento 2
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?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); |
Any doubts regarding this topic can be mentioned in the Comments section below and I’d be helping you out asap.
Please share the solution with the Magento community via social media.
Thank you.
Related Post:
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Override Product NewWidget Block in Magento 2
How to Add Stock Status Column in Product Grid in Magento 2
Next