// Magento XML products exporter
// by nirav chauhan
// http://niravchauhan.wordpress.com/

require_once ‘app/Mage.php’;
umask( 0 );
Mage::app( “default” );
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$_urlPath = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$_imagePath = $_urlPath . “media”;
$_logFileName = getcwd().”export_products.log”;
$_xmlPath = getcwd().”/var/export”;
$xmlFile = $_xmlPath . “/” . “new5.xml”;

$doc = new DomDocument(‘1.0’, ‘UTF-8’);
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
//$element = $doc->createElement(‘test’, ‘This is the root element!’);
$rss = $doc->createElement(‘rss’);
$doc->appendChild($rss);

$version = $doc->createAttribute(‘version’);
$rss->appendChild($version);

$value = $doc->createTextNode(‘2.0’);
$version->appendChild($value);

$xmlns_wp = $doc->createAttribute(‘xmlns:g’);
$rss->appendChild($xmlns_wp);

$value = $doc->createTextNode(‘http://base.google.com/ns/1.0’);
$xmlns_wp->appendChild($value);
// We insert the new element as root (child of the document)
// $doc->appendChild($element);
//$doc->createElementNS(‘http://base.google.com/ns/1.0’, ‘g:item_type’, ‘house’);
//$doc->simplexml_load_string(”);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace(‘g’, “http://base.google.com/ns/1.0”);
$chennel=$doc->CreateElement(‘chennel’);
$chennel = $rss->appendChild($chennel);
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 );
$_productCollection->count();
$i = 1;
foreach ( $_productCollection as $_product ) {

// Prepare array of variables to grow XML file
$v[‘g:id’] = $_product->getSku();
$v[‘title’] = $_product->getName();
// $v[‘type’] = $_product->getTypeId();
$v[‘link’] = $_urlPath . $_product->geturlpath();
$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[‘g:created_at’] = $_product->getCreatedAt();
$v[‘g:updated_at’] = $_product->getUpdatedAt();
$v[‘g:image_link’] = $_urlPath . $_product->geturlpath();
$v[‘g:image’] = $_imagePath . $_product->getImage();
$v[‘image_label’] = $_product->getImageLabel();
$v[‘g:price’] = $_product->getPrice();
$v[‘g:special_price’] = $_product->getSpecialPrice();
$v[‘g:shipping_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() == “giftcard” ) {
$_links = Mage::getModel(‘giftcard/product_type’)->getLinks( $_product );
foreach ( $_links as $_link )
$v[‘available_formats’][$_product->getSku()][] = $_link->getTitle();
}*/

// Prepare XML file to save

$root = $doc->createElement(‘item’);
$root = $chennel->appendChild($root);

/*$occ = $doc->createElement(‘root’);
$occ = $root->appendChild($occ);*/

foreach ( $v as $fieldName => $fieldValue ) {
$child = $doc->createElement($fieldName);
$child = $root->appendChild($child);

if ( is_array($fieldValue) ) {
$value = $doc->createCDATASection(implode( “|”, $fieldValue[$_product->getSku()] ));
$value = $child->appendChild($value);
} else {
$value = $doc->createCDATASection($fieldValue);
$value = $child->appendChild($value);
}
//$channel->appendChild($value);

}

}

echo $doc->save( $xmlFile ).’
‘;

Mage::log( “File ” . $i . “: ” . $_product->getSku(), null, $_logFileName );
$i++;

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

Link:Download
Download Here

By admin

One thought on “How to Export Product like Goodle Base Xml in Magento”

Leave a Reply

Your email address will not be published. Required fields are marked *