Magento export products in csv

Magento export products in csv : We sometimes need to export the magento products in files such as csv, xml. Here in this tutorial we are going to to explain how to export all the enabled products in csv file. We are going to create a script which will load product collection on the basis of the status filter. You can add your needed filter in product collection.

Magento export products in csv

Steps to export products in csv file in magento-

1. Create a file at root of magento named as exportProducts.php
Now add the below code in the file which contains the code to export the enabled products. The fields exported will be id, sku, name and price.

Magento export products in csv:

require_once 'app/Mage.php';
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToFilter('status', 1);
$fopen = fopen('products.csv', 'w');
$csvHeader = array("id","sku", "name", "price");// Add the fields you need to export
fputcsv( $fopen , $csvHeader,",");
foreach ($products as $product){
    $id = $product->getId();
	$sku = $product->getSku();
	$name = $product->getName();
	$price = $product->getPrice();
    fputcsv($fp, array($id,$sku,$name, $price), ",");Add the fields you added in csv header
fclose($fopen );

2. Run this file in browser such as :
When you run this file a csv file products.csv will be create which will look something like this –

Magento export products in csv

You can add the fields which you need to export in csv file. The above example exports only product id, sku, name and price.

Tip : You can add your own filter to get the products from database as per your requirement.
Note : If You have a lot products try incremental approach instead of loading all products at one time which will give better performance.


Add Comment

📖 Read More