2026 Executive BenchmarkMid-Market Enterprise Modernization: How leaders across unify Commerce, Odoo ERP & AI in 90 days.
MAGENTO COMMERCE

How to Export Simple Product CSV from Magento 2 Programmatically

How to Export Simple Product CSV Programmatically using custom Scripts. Create one custom.php File into the root folder. and put the below code into a file and run it into the brow...

Akshar Group Technologies
Dec 25, 2020
1 min read
Verified Source

How to Export Simple Product CSV Programmatically using custom Scripts.

Create one custom.php File into the root folder.

and put the below code into a file and run it into the browser.

You can find this CSV inside the root directory

php
<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require realpath(__DIR__) . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$registry = $objectManager->get('Magento\Framework\Registry');
$registry->register('isSecureArea', true);
//Store id of exported products, This is useful when we have multiple stores.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

/** @var MagentoCatalogModelResourceModelProductCollection $productCollection */
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection = $productCollection->addAttributeToSelect('*')->load();

$fp = fopen('exports.csv', 'w');
$csvHeader = array("name","sku");
$i = 0;

fputcsv( $fp, $csvHeader,",");
foreach ($collection as $product){
$data = array();
$data[] = $product->getName();
$data[] = $product->getSku();
fputcsv($fp, $data);
}

fclose($fp);
echo 'export all product';
?>
Share this playbook
LET'S BUILD WHAT'S NEXT

What's Holding Your Business Back?

Tell us where you want to go, what's getting in the way, and we'll help you figure out the technology to get there.

No sales pitch. Confidential strategic advisory.