Magento: Export All Products

Please find the sample code to export the products to CSV in Magento.
<?php
//export the products to CSV

 

require_once('app/Mage.php');
umask(0);

if (!Mage::isInstalled()) {
 echo "Application is not installed yet, please complete install wizard first.";
 exit;
}

// Only for urls // Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

Mage::app('admin')->setUseSessionInUrl(false);
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
error_reporting(E_ALL);

try {
 Mage::getConfig()->init();
 Mage::app();
} catch (Exception $e) {
 Mage::printException($e);
}
ini_set('memory_limit', '1024M');
ini_set('max_execution_time', 0);

$productCount = 0;
try {
 $storeId = Mage::app()->getStore()->getStoreId();
 //configure the collection filters.
 if (Mage::helper('catalog/product_flat')->isEnabled()) {
 //load a flat collection
 //$collection = Mage::getModel('catalog/product_flat')->getCollection();
 } else {
 $collection = Mage::getModel('catalog/product')->getCollection();

 $collection->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
 $collection->joinField('is_in_stock', 'cataloginventory/stock_item', 'is_in_stock', 'product_id=entity_id',
 '{{table}}.stock_id=1', 'left');
 $collection->joinField('websites', 'catalog/product_website', 'GROUP_CONCAT(DISTINCT at_websites.website_id) as websites',
 'product_id=entity_id', null, 'left');
 //joinField($alias, $table, $field, $bind, $cond=null, $joinType='inner');----syntax
 $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('description', 'catalog_product/description', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('short_description', 'catalog_product/short_description', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('tax_class_id', 'catalog_product/tax_class_id', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('weight', 'catalog_product/weight', 'entity_id', null, 'left', $storeId);
 $collection->joinField('category_ids', 'catalog/category_product', 'GROUP_CONCAT(DISTINCT at_category_ids.category_id)
 as category_ids', 'product_id=entity_id', null, 'left');
 $collection->joinField('child_ids', 'catalog/product_relation', 'GROUP_CONCAT(DISTINCT at_child_ids.child_id)
 as child_ids', 'parent_id=entity_id', null, 'left');
 $collection->joinAttribute('color', 'catalog_product/color', 'entity_id', null, 'left', $storeId);
 $collection->joinAttribute('size', 'catalog_product/size', 'entity_id', null, 'left', $storeId);
 $collection->addAttributeToSort('type_id', 'DESC');
 $collection->getSelect()->group('e.entity_id');
 }

 echo $collection->getSelect();exit;
 //This is the file to append the output to.

 $baseFilePath = 'exported_products';
 if (!file_exists($baseFilePath)) {
 mkdir($baseFilePath, 0777, true);
 }

 $csvToCreate = $baseFilePath . '/exported_products.csv';
 $csvObj = new Varien_File_Csv();
 $csvData = array();
 $productHeader = array(
 'entity_id', 'entity_type_id', 'attribute_set_id', 'type_id', 'sku', 'has_options', 'required_options', 'created_at',
 'updated_at', 'qty', 'is_in_stock', 'websites', 'status', 'visibility', 'price', 'name', 'description', 'short_description',
 'tax_class_id', 'weight', 'category_ids', 'child_ids', 'color', 'size', 'configurable_data'
 );
 $csvData[] = $productHeader;
 foreach ($collection as $product) {
 echo '<pre>';print_r($product->getData());exit;
 $productArray = array();
 $productArray[] = $product['entity_id'];
 $productArray[] = $product['entity_type_id'];
 $productArray[] = $product['attribute_set_id'];
 $productArray[] = $product['type_id'];
 $productArray[] = $product['sku'];
 $productArray[] = $product['has_options'];
 $productArray[] = $product['required_options'];
 $productArray[] = $product['created_at'];
 $productArray[] = $product['updated_at'];
 $productArray[] = $product['qty'];
 $productArray[] = $product['is_in_stock'];
 $productArray[] = $product['websites'];
 $productArray[] = $product['status'];
 $productArray[] = $product['visibility'];
 $productArray[] = $product['price'];
 $productArray[] = $product['name'];
 $productArray[] = $product['description'];
 $productArray[] = $product['short_description'];
 $productArray[] = $product['tax_class_id'];
 $productArray[] = $product['weight'];
 $productArray[] = $product['category_ids'];
 $productArray[] = $product['child_ids'];
 $productArray[] = $product['color'];
 $productArray[] = $product['size'];
 if ($product['type_id'] == 'configurable') {
 $pro = Mage::getModel('catalog/product')->load($product['entity_id']);
 $_attributes = $pro->getTypeInstance(true)->getConfigurableAttributesAsArray($pro);
 $productArray[] = json_encode($_attributes);
 }
 $csvData[] = $productArray;
 $productCount++;
 }
 //write the data to CSV.
 $csvObj->saveData($csvToCreate, $csvData);
} catch (Exception $e) {
 //Mage::printException($e);
 echo $e->getMessage();
}
echo "<br/><br/>Saved $productCount products to csv file n";
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s