Thursday, December 1, 2011

Creating xml file of all products fields in magento programmatically

Hi,

below code is used to create a xml file of all product for some 3ed party site
place this code in magento project root folder






<?php
 define('MAGENTO', realpath(dirname(__FILE__)));
 require_once MAGENTO . '/app/Mage.php';
 Mage::app();
 $storename='default';
 $file = "products_xml.xml";
if (file_exists($file)) { unlink ($file); }
 
?>


    <?php


$products = Mage::getModel('catalog/product')
   ->getCollection()
   ->addAttributeToSelect('*')
   ->addAttributeToFilter('status', array('eq' => '1'))
   ->addAttributeToFilter('type_id', array('eq' => 'simple'));

//process your product collection as per your bussiness logic


  $doc = new DOMDocument();
  $doc->formatOutput = true;
  
  $productsX = $doc->createElement( "products" );
  $doc->appendChild( $productsX );
  
foreach($products as $_product){
//print_r($_product->getData()); exit;
 /*echo $_product->getSku().'</br>';
 echo $_product->getName().'</br>';
 echo $_product->getData('image').'</br>';
 echo $_product->getData('small_image').'</br>';
 echo $_product->getData('thumbnail').'</br>'; 
 echo $_product->getData("url_key").'</br>';
 echo $_product->getShippingDelivery().'</br>';
 echo $_product->getShippingWeight() .'</br>';
 echo $_product->getAlu().'</br>'; 
 echo $_product->getUpsize().'</br>';
 echo $_product->getPrice().'</br>';
 echo $_product->getSpecialPrice().'</br>'; 
 echo $_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product).'</br>'; 
 echo $_product->getResource()->getAttribute('status')->getFrontend()->getValue($_product).'</br>'; 
 echo $_product->getResource()->getAttribute('gender')->getFrontend()->getValue($_product).'</br>'; 
 echo $_product->getResource()->getAttribute('size')->getFrontend()->getValue($_product).'</br>';
 echo $_product->getResource()->getAttribute('season')->getFrontend()->getValue($_product).'</br>';
 echo $_product->getDescription().'</br>';
 echo Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty().'</br>'; 
 echo Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock().'</br>';
 exit; */
 $product = $doc->createElement( "product" );
  
 $sku = $doc->createElement( "sku" );
 $sku->appendChild(
  $doc->createTextNode($_product->getSku())
 );
 $product->appendChild( $sku );
  
 $name = $doc->createElement( "name" );
 $name->appendChild(
  $doc->createTextNode( trim($_product->getName()) )
 );
 $product->appendChild( $name );
  
 $image = $doc->createElement( "image" );
 $image->appendChild(
  $doc->createTextNode( trim($_product->getData('image')) )
 );
 $product->appendChild( $image );
  
 $smallimage = $doc->createElement( "smallimage" );
 $smallimage->appendChild(
  $doc->createTextNode( trim($_product->getData('small_image')) )
 );
 $product->appendChild( $smallimage );
  
 $thumbnail = $doc->createElement( "thumbnail" );
 $thumbnail->appendChild(
  $doc->createTextNode( trim($_product->getData('thumbnail')) )
 );
 $product->appendChild( $thumbnail );
  
 $urlkey = $doc->createElement( "urlkey" );
 $urlkey->appendChild(
  $doc->createTextNode( trim($_product->getData('url_key')) )
 );
 $product->appendChild( $urlkey );
  
 $shippingdelivery = $doc->createElement( "shippingdelivery" );
 $shippingdelivery->appendChild(
  $doc->createTextNode( trim($_product->getShippingDelivery()) )
 );
 $product->appendChild( $shippingdelivery );
  
 $shippingweight = $doc->createElement( "shippingweight" );
 $shippingweight->appendChild(
  $doc->createTextNode( trim($_product->getShippingWeight()) )
 );
 $product->appendChild( $shippingweight );
  
 $alu = $doc->createElement( "alu" );
 $alu->appendChild(
  $doc->createTextNode( trim($_product->getAlu()) )
 );
 $product->appendChild( $alu );
  
 $upsize = $doc->createElement( "upsize" );
 $upsize->appendChild(
  $doc->createTextNode( trim($_product->getUpsize()) )
 );
 $product->appendChild( $upsize );
  
 $price = $doc->createElement( "price" );
 $price->appendChild(
  $doc->createTextNode( trim($_product->getPrice()) )
 );
 $product->appendChild( $price );
  
 $specialprice = $doc->createElement( "specialprice" );
 $specialprice->appendChild(
  $doc->createTextNode( trim($_product->getSpecialPrice()) )
 );
 $product->appendChild( $specialprice );
  
 $color = $doc->createElement( "color" );
 $color->appendChild(
  $doc->createTextNode( trim($_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)))
 );
 $product->appendChild( $color );
  
 $status = $doc->createElement( "status" );
 $status->appendChild(
  $doc->createTextNode( trim($_product->getResource()->getAttribute('status')->getFrontend()->getValue($_product)) )
 );
 $product->appendChild( $status );
  
 $gender = $doc->createElement( "gender" );
 $gender->appendChild(
  $doc->createTextNode( trim($_product->getResource()->getAttribute('gender')->getFrontend()->getValue($_product)) )
 );
 $product->appendChild( $gender );
  
 $size = $doc->createElement( "size" );
 $size->appendChild(
  $doc->createTextNode( trim($_product->getResource()->getAttribute('size')->getFrontend()->getValue($_product)) )
 );
 $product->appendChild( $size );
  
 $season = $doc->createElement( "season" );
 $season->appendChild(
  $doc->createTextNode( trim($_product->getResource()->getAttribute('season')->getFrontend()->getValue($_product)) )
 );
 $product->appendChild( $season );
  
 $description = $doc->createElement( "description" );
 $description->appendChild(
  $doc->createTextNode( trim($_product->getDescription()) )
 );
 $product->appendChild( $description );
  
 $qty = $doc->createElement( "qty" );
 $qty->appendChild(
  $doc->createTextNode( trim(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()) )
 );
 $product->appendChild( $qty );
  
 $availability = $doc->createElement( "availability" );
 $availability->appendChild(
  $doc->createTextNode( trim(Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getIsInStock()) )
 );
 $product->appendChild( $availability );

   $productsX->appendChild($product);  
    }
  //echo $doc->saveXML();
 file_put_contents($file,$doc->saveXML(),FILE_APPEND);
     ?>


you can check this post http://www.magentocommerce.com/boards/viewthread/267477/

No comments:

Post a Comment