Monday 28 September 2015

Creating xml file of all products fields in magento programmatically

I will be using Varien_Simplexml_Element class to read write xml nodes. The path to this class file is lib/Varien/Simplexml/Element.php

Here is a sample XML file which I am going to read through Magento code. I will also be adding an XML node to the following XML data.

$file = "products.xml";
        if (file_exists($file)) { unlink ($file); }
       
        $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){

            $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 );

            $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); 
        }
        file_put_contents($file,$doc->saveXML(),FILE_APPEND);

No comments:

Post a Comment