Magento Export products in custom XML [script]

If you have to export all or a part of your Magento products you can try this simple script which creates a products collection and writes a simple and custom XML file per product (but you can simply change it to write a single long XML file).

Hope you enjoy it!

<?php
// Magento XML products exporter
// Version 0.1
// by Michele Marcucci
// http://www.michelem.org

require_once 'app/Mage.php';
umask( 0 );
Mage::app( "default" );
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$_magentoPath = "CHANGETHISTOYOURMAGENTOPATH ex: /var/virtual/magento"
$_urlPath = "";
$_imagePath = $_urlPath . "media";
$_logFileName = "export_products.log";
$_xmlPath = $_magentoPath . "/var/export";

Mage::log( "Export start", null, $_logFileName );

// Prepare collection
$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect('*');

/* You can change and uncomment these lines below to filter your products collection */

// Filter by updated_at date, get only daily changes
//$_productCollection->addFieldToFilter('updated_at', array('from'=>date("Y-m-d", time()-86400)));

// Filter by product type, get only downloadables
//$_productCollection->addFieldToFilter('type_id',  array('like'=>'downloadable'));

// Filter by sku get only products with sku like "EBOOK-%"
//$_productCollection->addFieldToFilter('sku',  array('like'=>'EBOOK-%'));

// Limit output to 15 records
//$_productCollection->getSelect()->limit(15);

Mage::log( "Products to be exported: " . $_productCollection->count(), null, $_logFileName );

$i = 1;
foreach ( $_productCollection as $_product ) {

	// Prepare array of variables to grow XML file
    $v['sku'] = $_product->getSku();
    $v['product_name'] = $_product->getName();
	$v['type'] = $_product->getTypeId();
    $v['description'] = $_product->getDescription();
    $v['short_description'] = $_product->getShortDescription();
    $v['meta_title'] = $_product->getMetaTitle();
    $v['meta_description'] = $_product->getMetaDescription();
    $v['meta_keyword'] = $_product->getMetaKeyword();
    $v['created_at'] = $_product->getCreatedAt();
    $v['updated_at'] = $_product->getUpdatedAt();
    $v['url_path'] = $_urlPath . $_product->geturlpath();
    $v['image'] = $_imagePath . $_product->getImage();
    $v['image_label'] = $_product->getImageLabel();
  	$v['price'] = $_product->getPrice();
   	$v['special_price'] = $_product->getSpecialPrice();
    $v['weight'] = $_product->getWeight();

	// Get the Magento categories for the product
    $categoryIds = $_product->getCategoryIds();

	foreach($categoryIds as $categoryId) {
    	$category = Mage::getModel('catalog/category')->load($categoryId);
    	$v['categories'][$_product->getSku()][] = $category->getName();
	}

    // If product is downloadable get some informations about links added
    if ( $_product->getTypeId() == "downloadable" ) {
    	$_links = Mage::getModel('downloadable/product_type')->getLinks( $_product );
    	foreach ( $_links as $_link )
    		$v['available_formats'][$_product->getSku()][] = $_link->getTitle();
    }

	// Prepare XML file to save
	$xmlFile = $_xmlPath . "/" . $_product->getSku() . ".xml";
	
	$doc = new DomDocument('1.0', 'UTF-8');
	$doc->preserveWhiteSpace = false;
	$doc->formatOutput = true;	
	
	$root = $doc->createElement('product');
	$root = $doc->appendChild($root);
	
	$occ = $doc->createElement('root');
	$occ = $root->appendChild($occ);
	
	foreach ( $v as $fieldName => $fieldValue ) {
		$child = $doc->createElement($fieldName);
    	$child = $occ->appendChild($child);

		if ( is_array($fieldValue) ) {
			$value = $doc->createTextNode(implode( "|", $fieldValue[$_product->getSku()] ));
			$value = $child->appendChild($value);
		} else {
			$value = $doc->createTextNode($fieldValue);
			$value = $child->appendChild($value);
		}

	}

	// Save each product as XML file
	$doc->save( $xmlFile );
	
	Mage::log( "File " . $i . ": " . $_product->getSku(), null, $_logFileName );
	
	$i++;
}

Mage::log( "Export done", null, $_logFileName );

Tags: , , , , , , ,

Post simili:

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

17 Responses to “Magento Export products in custom XML [script]”
  1. tojas
    12:35 on November 28th, 2012

    I tried to use it but it gets this error:

    Fatal error: Uncaught exception ‘Mage_Core_Model_Store_Exception’ in /var/www/data1/webpages/magento/app/code/core/Mage/Core/Model/App.php:1318 Stack trace: #0 /var/www/data1/webpages/magento/app/code/core/Mage/Core/Model/App.php(830): Mage_Core_Model_App->throwStoreException() #1 /var/www/data1/webpages/magento/app/code/core/Mage/Core/Model/App.php(481): Mage_Core_Model_App->getStore() #2 /var/www/data1/webpages/magento/app/code/core/Mage/Core/Model/App.php(269): Mage_Core_Model_App->_initCurrentStore(‘default’, ‘store’) #3 /var/www/data1/webpages/magento/app/Mage.php(583): Mage_Core_Model_App->init(‘default’, ‘store’, Array) #4 /var/www/data1/webpages/magento/_xml_export.php(9): Mage::app(‘default’) #5 {main} thrown in /var/www/data1/webpages/magento/app/code/core/Mage/Core/Model/App.php on line 1318

    magento version is 1.6.2
    any solution?

    thanks in advance!

  2. Heine
    14:17 on September 4th, 2012

    Is this script still working? Im having trouble getting it to work.

  3. Jhone
    16:45 on February 28th, 2012

    How to generate an xml for all products?

  4. Marius
    17:58 on December 31st, 2011

    Thanks Michele! You really help my with this script. I put your blog in favorite.

  5. bygbyt
    22:57 on December 20th, 2011

    Parece dificil de inplementar ,Intento a ver que sale de lo consigo

  6. Buy TRX
    04:19 on December 12th, 2011

    Nice read. Thanks for the info

  7. Block Websites
    09:32 on October 21st, 2011

    Very informative article. Thanks for sharing this article.

  8. Property Marbella
    15:40 on September 26th, 2011

    Very informative post, Tanks for sharing.

  9. vipin sahu
    14:24 on June 8th, 2011

    Awesome information .THANKS!

  10. Dave
    03:50 on June 4th, 2011

    How well do you like this? I have seen Magneto talked about on-line, but no one has really given a good review. Thoughts?

  11. Home Protect Home Warranty
    12:06 on April 29th, 2011

    Well thanks for the information that If you have to export all or a part of your Magento products you can try this simple script which creates a products collection and writes a simple and custom XML file per product…

  12. kapamaroy
    12:18 on April 14th, 2011

    Could you also explain how to use this file? In which directory should it be placed and how to execute the script in order to generate the xml files?

  13. ravi
    19:54 on April 9th, 2011

    Very Good article

    Thanks
    JeevanSathi

  14. LocalPosition.com
    07:23 on March 15th, 2011

    Very informative post.Thanks for posting.

  15. Villaggi tropea
    19:08 on February 22nd, 2011

    molto interessante e utile! Grazie ciao

  16. perros
    06:30 on January 25th, 2011

    Tanks for sharing”!

  17. centro osteopatico a roma
    18:57 on December 27th, 2010

    Grazie !!! E auguri a voi

Leave a comment

(required)

(required)


*

Categories