Studyon Minte9.com
Magento




Feed - export products



<?php 
        
        // http://your_shop.com/export_products.php

	set_time_limit(0);
	require_once 'app/Mage.php';
	Mage::app();

	class Export
	{
		function generateTextXMLOutput()
		{
			$default_currency = Mage::getModel('directory/currency')->getConfigBaseCurrencies();
			$datafeed_default_currency = $default_currency[0];
			$currency_value_rate = Mage::getModel('directory/currency')
                                       ->getCurrencyRates($datafeed_default_currency, 'RON');

			// products GET
			$products = Mage::getModel('catalog/product')->getCollection();
			$products->addAttributeToFilter('status', 1);//enabled
			$products->addAttributeToFilter('visibility', 4); //catalog, search
			$products->addAttributeToSelect('*');
			$prodIds = $products->getAllIds();

			$product = Mage::getModel('catalog/product');

			$i = 0;
			$special_from_date	= '';
			$special_to_date	= '';
			foreach($prodIds as $productId) {

				$product->load($productId);				
				
				// price		
				$_taxHelper  = Mage::helper('tax');

				if ( $product->getSpecialPrice() && 
				            (date("Y-m-d G:i:s") > $product->getSpecialFromDate() || 
				            !$product->getSpecialFromDate()) &&  
				            (date("Y-m-d G:i:s") < $product->getSpecialToDate() || 
				            !$product->getSpecialToDate())){
				                 $price = $product->getSpecialPrice();
				} else {
				                 $price = $product->getPrice();
				}
			       $finalPrice = $_taxHelper->getPrice($product, $price, true);

				// category
				foreach($product->getCategoryIds() as $_categoryId) {
					$category = Mage::getModel('catalog/category')->load($_categoryId);
					if ($category->getName()) 
						$categoryName .= ($nume_categorie == '') ? 
                                                    $category->getName() : '>'.$category->getName();
				}
				$nume_categorie = 
                                   htmlentities(strip_tags(str_replace($search, $replace, trim($nume_categorie))));

				// manufacturer
				if ($product->getResource()->getAttribute('manufacturer')) {
					$marca = $product->getResource()->
                                                   getAttribute('manufacturer')->getFrontend()->getValue($product);
				}

				// product node
				$productArr = array(
					'SKU'				=> htmlentities(strip_tags(trim($product->getId()))),
					'ManufactureName' 	=> $marca,
					'ProductName'		=> htmlentities(strip_tags(
                                                    str_replace($search, $replace, trim($product->getName())))),
					//'Description'		=> htmlentities(strip_tags(
                                                     trim($product->getDescription()))),
					'URL'				=> htmlentities(strip_tags(
                                                     trim($product->getProductUrl()))),
					'CategoryName'		=> $categoryName,
					'Price'				=> $finalPrice,
					'InStock'			=> '1',
					'UrlImage'			=> htmlentities(strip_tags(trim(
                                                    Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
                                                   'catalog/product'.$product->getImage()))),
				);

				$productNode = "\t<product>\n";
				foreach ($productArr as $k => $v) {
					if ($v != '' and $v != -1)
					{
						$productNode .= "\t\t<$k>$v</$k>\n";
					}
				}
				$productNode .= "\t</product>\n";

				if ($i == 0) {
					$xmlHead = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n
<products xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
xsi:noNamespaceSchemaLocation=\"http://www.yoursite.com/products.xsd\">\n";
					echo $xmlHead;
					flush();
				}
				
				echo $productNode;					
				flush();							

				$i++;								
				
				if ($i>1) break;
			}

			$xmlFoot = "</products>";
			echo $xmlFoot;
			flush();
		}

	}

	$exporter = new Export();
	$exporter->generateTextXMLOutput();



/* Output

<?xml version="1.0" encoding="UTF-8" ?>
<products 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="http://www.yoursite.com/products.xsd">
	<product>
		<SKU>299</SKU>
		<ManufactureName>Pro Plan</ManufactureName>
		<ProductName>Pro Plan Cat Adult Pui,Orez 7,5kg</ProductName>
		<URL>http://shop.com/pro-plan-cat-adult-pui-7kg.html</URL>' target='_blank'>http://shop.com/pro-plan-cat-adult-pui-7kg.html</URL>
		<CategoryName>Adult</CategoryName>
		<Price>158.72</Price>
		<InStock>1</InStock>
		<UrlImage>http://shop.com/media/catalog/product/p/r/proplan.jpg</UrlImage>
	</product>
	<product>
		<SKU>300</SKU>
		<ManufactureName>Pro Plan</ManufactureName>
		<ProductName>Pro Plan Cat Light Curcan 7,5kg</ProductName>
		<URL>http://shop.com/pro-plan-cat-adult-pui-7kg.html</URL>' target='_blank'>http://shop.com/pro-plan-cat-adult-pui-7kg.html</URL>
		<CategoryName>AdultAdult</CategoryName>
		<Price>158.72</Price>
		<InStock>1</InStock>
		<UrlImage>http://shop.com/media/catalog/product/f/i/file_757_1.jpg</UrlImage>
	</product>
</products>

*/