• 28th, May 2011

Magento Schedule Export Google Base products with Cron

Schedule the script bellow every day with cron in your system like
01 00 * * * php -f /srv/www/htdocs/Magento/exportGoogleBase.php

define('SAVE_FEED_LOCATION','/srv/www/htdocs/Magento/var/export/google_base_feed.txt'); //you can set a new folder and file if you want, don't forget to chmod the folder to 777

function cleanString($text) {
	// 1) convert á ô => a o
	$text = preg_replace("/[áàâãªä]/u","a",$text);
	$text = preg_replace("/[ÁÀÂÃÄ]/u","A",$text);
	$text = preg_replace("/[ÍÌÎÏ]/u","I",$text);
	$text = preg_replace("/[íìîï]/u","i",$text);
	$text = preg_replace("/[éèêë]/u","e",$text);
	$text = preg_replace("/[ÉÈÊË]/u","E",$text);
	$text = preg_replace("/[óòôõºö]/u","o",$text);
	$text = preg_replace("/[ÓÒÔÕÖ]/u","O",$text);
	$text = preg_replace("/[úùûü]/u","u",$text);
	$text = preg_replace("/[ÚÙÛÜ]/u","U",$text);
	$text = preg_replace("/[’‘‹›‚]/u","'",$text);
	$text = preg_replace("/[“”«»„]/u",'"',$text);
	$text = str_replace("–","-",$text);
	$text = str_replace(" "," ",$text);
	$text = str_replace("ç","c",$text);
	$text = str_replace("Ç","C",$text);
	$text = str_replace("ñ","n",$text);
	$text = str_replace("Ñ","N",$text);

	//2) Translation CP1252. – => -
	$trans = get_html_translation_table(HTML_ENTITIES);
	$trans[chr(130)] = '‚';    // Single Low-9 Quotation Mark
	$trans[chr(131)] = 'ƒ';    // Latin Small Letter F With Hook
	$trans[chr(132)] = '„';    // Double Low-9 Quotation Mark
	$trans[chr(133)] = '…';    // Horizontal Ellipsis
	$trans[chr(134)] = '†';    // Dagger
	$trans[chr(135)] = '‡';    // Double Dagger
	$trans[chr(136)] = 'ˆ';    // Modifier Letter Circumflex Accent
	$trans[chr(137)] = '‰';    // Per Mille Sign
	$trans[chr(138)] = 'Š';    // Latin Capital Letter S With Caron
	$trans[chr(139)] = '‹';    // Single Left-Pointing Angle Quotation Mark
	$trans[chr(140)] = 'Œ';    // Latin Capital Ligature OE
	$trans[chr(145)] = '‘';    // Left Single Quotation Mark
	$trans[chr(146)] = '’';    // Right Single Quotation Mark
	$trans[chr(147)] = '“';    // Left Double Quotation Mark
	$trans[chr(148)] = '”';    // Right Double Quotation Mark
	$trans[chr(149)] = '•';    // Bullet
	$trans[chr(150)] = '–';    // En Dash
	$trans[chr(151)] = '—';    // Em Dash
	$trans[chr(152)] = '˜';    // Small Tilde
	$trans[chr(153)] = '™';    // Trade Mark Sign
	$trans[chr(154)] = 'š';    // Latin Small Letter S With Caron
	$trans[chr(155)] = '›';    // Single Right-Pointing Angle Quotation Mark
	$trans[chr(156)] = 'œ';    // Latin Small Ligature OE
	$trans[chr(159)] = 'Ÿ';    // Latin Capital Letter Y With Diaeresis
	$trans['euro'] = '€';    // euro currency symbol
	ksort($trans); 

	foreach ($trans as $k => $v) {
		$text = str_replace($v, $k, $text);
	}

	// 3) remove <p>, <br/> ...
	$text = strip_tags($text); 

	// 4) &amp; => & &quot; => '
	$text = html_entity_decode($text);

	// 5) remove Windows-1252 symbols like "TradeMark", "Euro"...
	$text = preg_replace('/[^(\x20-\x7F)]*/','', $text); 

	return ($text);
} 

// make sure we don't time out
set_time_limit(0);

require_once '/srv/www/htdocs/Magento/app/Mage.php';
Mage::app('default');

try{
	$handle = fopen(SAVE_FEED_LOCATION, 'w');

	$heading = array('id','title','description','link','image_link','price','condition','c:product_code');
	$feed_line=implode("\t", $heading)."\r\n";
	fwrite($handle, $feed_line);

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

	$i = 0;
	foreach($prodIds as $productId) {
		$product = Mage::getModel('catalog/product');
		$product->load($productId);
			$i++;
			$product_data = array();
			$product_data['id']=$product->getId();
			$product_data['title']=$product->getName();
			$product_data['description']=$product->getDescription();
			$product_data['link']=$product->getProductUrl();
			$product_data['image_link']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
			if($product->getSpecialPrice())
			$product_data['price']=round($product->getSpecialPrice(),2);
			else
			$product_data['price']=round($product->getPrice(),2);
			$product_data['condition']='new';
			$product_data['c:product_code']=$product->getSku();

			//sanitize data
			foreach($product_data as $k=>$val){
				$bad=array('"',"\r\n","\n","\r","\t");
				$good=array(""," "," "," ","");
				$val = str_replace($bad,$good,$val);
				$val = cleanString($val); // convert á ô ... => a o
				$product_data[$k] = $val;
			}

			$feed_line = implode("\t", $product_data)."\r\n";
			fwrite($handle, $feed_line);
			fflush($handle);

			unset($product);
	}
	fclose($handle);
}
catch(Exception $e){
	die($e->getMessage());
}

Then set up your Google Base merchant account like the pic bellow

Google Base Magento settings

Tags: , ,

8 Responses to “Magento Schedule Export Google Base products with Cron”


  1. Jens
    on Jun 11th, 2011
    @ 1:44 pm

    Thanks for your great script. It is very useful.

    Unfortunately I get this message if I try to run it:

    Fatal error: Uncaught exception ‘Mage_Core_Model_Store_Exception’ in /www/htdocs/xxxxx/magento/app/code/core/Mage/Core/Model/App.php:1291 Stack trace:
    #0 /www/htdocs/xxxxx/magento/app/code/core/Mage/Core/Model/App.php(803): Mage_Core_Model_App->throwStoreException()
    #1 /www/htdocs/xxxxx/magento/app/code/core/Mage/Core/Model/App.php(454): Mage_Core_Model_App->getStore()
    #2 /www/htdocs/xxxxx/magento/app/code/core/Mage/Core/Model/App.php(262): Mage_Core_Model_App->_initCurrentStore(‘default’, ‘store’)
    #3 /www/htdocs/xxxxx/magento/app/Mage.php(570): Mage_Core_Model_App->init(‘default’, ‘store’, Array)
    #4 /www/htdocs/xxxxx/xxxxx/GoogleBase.php(74): Mage::app(‘default’)
    #5 {main} thrown in /www/htdocs/xxxxx/magento/app/code/core/Mage/Core/Model/App.php on line 1291

    Do you know what is going wrong?

    Best Regards from Hamburg


  2. admin
    on Jun 13th, 2011
    @ 9:13 pm

    Hi Jens, I think is related to

    Mage::app(‘default’);

    try instead to replace with the Mage:app line that you will find in your index.php

    I was running 1.4.0.1 with this script.

    Best regards from Colombia!


  3. Martijn
    on Jun 16th, 2011
    @ 11:27 am

    Nice script! Only needed a few small tweaks to get it to export to the Dutch Google Base store.


  4. Jens
    on Jun 16th, 2011
    @ 10:12 pm

    Hi,

    thanks for your quick reply. I’m using 1.5.1.0.
    In index.php is no direct call for Mage::app. This is the code:

    —begin—
    $mageFilename = ‘app/Mage.php’;
    $maintenanceFile = ‘maintenance.flag’;

    if (!file_exists($mageFilename)) {
    if (is_dir(‘downloader’)) {
    header(“Location: downloader”);
    } else {
    echo $mageFilename.” was not found”;
    }
    exit;
    }

    if (file_exists($maintenanceFile)) {
    include_once dirname(__FILE__) . ‘/errors/503.php’;
    exit;
    }

    require_once $mageFilename;

    #Varien_Profiler::enable();

    if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
    Mage::setIsDeveloperMode(true);
    }

    #ini_set(‘display_errors’, 1);

    umask(0);

    /* Store or website code */
    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : ”;

    /* Run store or run website */
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : ‘store’;

    Mage::run($mageRunCode, $mageRunType);
    —end—

    Best Regards
    Jens


  5. me
    on Jun 20th, 2011
    @ 5:06 pm

    Nice script!
    Wondering if you can help me figure out why the txt file gets created, but it only has the header row (no items at all). Running magento 1.5


  6. me
    on Jun 20th, 2011
    @ 5:09 pm

    actually, the last field in the header says : c:product_code

    and there are lots of blank links underneath, consisting of a bunch of spaces


  7. Cds
    on Jul 11th, 2011
    @ 11:58 pm

    Hello,
    Good script,
    However I got small problem, my txt file generate got only the 1st row (headers) and nothing else.
    Any idea ?
    Thanks


  8. admin
    on Jul 13th, 2011
    @ 8:05 pm

    Try to do a die() statement through the script to see what’s going on. Also var_dump() could help you…

Leave a Reply

*

© 2010 unexpected[it]. All Rights Reserved.