jruzafa
10/12/2013 - 11:37 AM

Feed products magento

Feed products magento

<?php 
/*
ini_set('display_errors',1);
error_reporting(E_ALL);
*/
@apache_setenv('no-gzip',1);
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);

function flush_buffers(){
  ob_end_flush();
	ob_flush();
	flush();
	ob_start();
}

define('SAVE_FEED_LOCATION','google_base_feed.xml');
set_time_limit();
require_once '../app/Mage.php';
Mage::app(); 

try  {
	$handle = fopen(SAVE_FEED_LOCATION, 'w');
	fwrite($handle,"<?xml version=\"1.0\"?>\n");
	fwrite($handle,"<rss xmlns:g=\"http://base.google.com/ns/1.0\" version=\"2.0\">\n");
	fwrite($handle,"<channel>\n");
	
	
	$products = Mage::getModel('catalog/product')->getCollection();
	$products->addAttributeToFilter('status',1);
	$products->addAttributeToFilter('visibility',4);
	$products->addAttributeToSelect('sku');
	$products->addAttributeToSelect('id');
	$products->addAttributeToSelect('name');
	$products->addAttributeToSelect('description');
	$products->addAttributeToSelect('short_description');
	$products->addAttributeToSelect('url');
	$products->addAttributeToSelect('image');
	$products->addAttributeToSelect('price');
	$products->addAttributeToSelect('special_price');
	$products->addAttributeToSelect('manufacturer');
	$products->addAttributeToSelect('category_ids');
	$products->addAttributeToSelect('color');
	$products->addAttributeToSelect('marca');
	$products->addAttributeToSelect('sexo');
	$products->addAttributeToSelect('categoria_google');
	
	$cuenta = count($products);
	echo "Actualizados $cuenta productos correctamente<br />";
	
	foreach($products as $product){		
		$product_title = $product->getName();
		$product_id = $product->getId();
		$product_link = $product->getProductUrl();
		$description = str_replace('<p>&#160;</p>','',$product->getDescription());
		if ($description == "" || empty($description) || !$description){
			$product_description = str_replace('<p>&#160;</p>','',iconv("UTF-8","UTF-8//IGNORE",$product->getShortDescription()));
		} else {
			$product_description = iconv("UTF-8","UTF-8//IGNORE",$description);
		}
		if ($product->getSpecialPrice()!=NULL){
			$product_special_price = number_format(round($product->getSpecialPrice(),2),2,',','');
		} else {
			$product_special_price = "";
		}
		$product_price = number_format(round($product->getPrice(),2),2,',','');
		$product_image_link = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'. $product->getImage();
		$product_colores = $product->getResource()->getAttribute('color')->getFrontend()->getValue($product);
		$product_marca = $product->getResource()->getAttribute('marca')->getFrontend()->getValue($product);
		$product_google_category = $product->getData('categoria_google');
		$product_type = Mage::getModel("eav/entity_attribute_set")->load($product->getAttributeSetId())->getAttributeSetName();

		//$product_type = $product->getResource()->getData('attribute_set')->getFrontend()->getValue($product);
		switch ($product->getResource()->getAttribute('sexo')->getFrontend()->getValue($product)){
			case "Hombre":
				$product_sexo = "male";
				$product_edad = "adult";
				break;
			case "Mujer":
				$product_sexo = "female";
				$product_edad = "adult";
				break;
			case "Adulto":
				$product_sexo = "unisex";
				$product_edad = "adult";
				break;
			case "Niño":
				$product_sexo = "unisex";
				$product_edad = "kids";
				break;
		}

		/* ESCRIBIMOS EL FEED */
			
			fwrite($handle,"\t<item>\n"); 
			fwrite($handle,"\t\t<title><![CDATA[".$product_title."]]></title>\n");//OK
			fwrite($handle,"\t\t<link>".$product_link."</link>\n");
			fwrite($handle,"\t\t<description><![CDATA[".$product_description."]]></description>\n");//OK
			fwrite($handle,"\t\t<g:google_product_category><![CDATA[".$product_google_category."]]></g:google_product_category>\n");//Añadir
			fwrite($handle,"\t\t<g:product_type>".$product_type."</g:product_type>\n");//Añadir
			fwrite($handle,"\t\t<g:brand>".$product_marca."</g:brand>\n");//OK
			fwrite($handle,"\t\t<g:gender>".$product_sexo."</g:gender>\n");//OK
			fwrite($handle,"\t\t<g:age_group>".$product_edad."</g:age_group>\n");//OK
			fwrite($handle,"\t\t<g:color><![CDATA[".$product_colores."]]></g:color>\n");//OK
			fwrite($handle,"\t\t<g:size></g:size>\n");//Añadir (separados por /)
			fwrite($handle,"\t\t<g:shipping>\n");//OK 
			fwrite($handle,"\t\t\t<g:country>ES</g:country>\n");//OK 
			fwrite($handle,"\t\t\t<g:service>Estandar</g:service>\n");//OK 
			fwrite($handle,"\t\t\t<g:price>0.00</g:price>\n");//OK 
			fwrite($handle,"\t\t</g:shipping>\n");//OK
			fwrite($handle,"\t\t<g:id>".$product_id."</g:id>\n");//OK
			fwrite($handle,"\t\t<g:price>".$product_price."</g:price>\n");//OK
			if ($product_special_price!=""){
				fwrite($handle,"\t\t<g:sale_price>".$product_special_price."</g:sale_price>\n");//OK
				fwrite($handle,"\t\t<g:sale_price_effective_date>2012-01-01T00:00-0000/2020-12-31T00:00-0000</g:sale_price_effective_date>\n");//OK
			}
			fwrite($handle,"\t\t<g:image_link>".$product_image_link."</g:image_link>\n");//OK
			/* Bucle para imágenes adicionales*/
			fwrite($handle,"\t\t<g:additional_image_link>".$product_image_link."</g:additional_image_link>\n");//OK
			/*Fin bucle*/
			fwrite($handle,"\t\t<g:condition>new</g:condition>\n");//OK
			fwrite($handle,"\t\t<g:availability>in stock</g:availability>\n");//OK
			fwrite($handle,"\t</item>\n");
		/* FIN ESCRITURA FEED */
		fflush($handle);
	}
	fwrite($handle,"</channel>");
	fwrite($handle,"</rss>");
	fclose($handle);
	
}
catch(Exception $e){
	die($e->getMessage());
}