Skip to content

Instantly share code, notes, and snippets.

@mklooss
Last active June 7, 2017 21:23
Show Gist options
  • Save mklooss/d10bdf4f8aa325f99862 to your computer and use it in GitHub Desktop.
Save mklooss/d10bdf4f8aa325f99862 to your computer and use it in GitHub Desktop.
Magento Google Shopping - Simple Script
<?php
if(php_sapi_name() != 'cli')
{
die('only via cli');
}
require 'app/Mage.php';
umask(0);
error_reporting(E_ALL);
ini_set('display_errors', 'on');
Mage::app('admin');
$resource = Mage::getSingleton('core/resource');
/* @var $resource Mage_Core_Model_Resource */
$conn = $resource->getConnection('core_write');
/* @var $conn Varien_Db_Adapter_Interface */
$dir = Mage::getBaseDir().DS.'export';
if(!is_dir($dir))
{
mkdir($dir, 0755, true);
}
$newFile = $dir . DS . 'googleshopping.xml';
$tmpFile = $newFile . '.tmp';
$shipping = round(6.90, 2);
$fp = fopen($tmpFile, 'w');
fwrite($fp, '<?xml version="1.0" encoding="UTF-8"?>'."\n");
fwrite($fp, '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">'."\n");
fwrite($fp, '<title>Stopperka.de</title>'."\n");
fwrite($fp, '<link rel="self" href="http://www.stopperka.de"/>'."\n");
fwrite($fp, '<updated>'.date('Y-m-d').'T'.date('H:i:s').'Z'.'</updated>'."\n");
fwrite($fp, '<author>'."\n");
fwrite($fp, '<name>Stopperka.de</name>'."\n");
fwrite($fp, '</author>'."\n");
fwrite($fp, '<id>stopperka-de-shopping</id>'."\n");
fwrite($fp, ''."\n");
$categories = array();
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect(array('name', 'google_category'));
foreach($collection as $_categorie)
{
$categories[$_categorie->getId()] = $_categorie->getGoogleCategory();
}
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addStoreFilter(2)
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToFilter('type_id', 'simple')
->addAttributeToFilter('status', 1)
->addUrlRewrite();
foreach($collection as $_product)
{
$query = $conn->select()
->from($resource->getTableName('catalog_category_product'), 'category_id')
->where('product_id = ?', $_product->getId());
$ids = $conn->fetchCol($query);
$_product->setCategoryIds($ids);
$cat = '';
foreach($ids as $id)
{
if(isset($categories[$id]))
{
$cat = $categories[$id];
break;
}
}
if(empty($cat) || is_null($_product->getData('ean')))
{
continue;
}
fwrite($fp, '<entry>'."\n");
$qty = (int) $_product->getQty();
$stock = 'out of stock';
if($qty > 0)
{
$stock = 'in stock';
}
$_priceIncludingTax = Mage::helper('tax')->getPrice($_product, $_product->getFinalPrice());
fwrite($fp, '<title><![CDATA['.$_product->getName().']]></title>'."\n");
fwrite($fp, '<g:brand><![CDATA['.$_product->getAttributeText('manufacturer').']]></g:brand>'."\n");
fwrite($fp, '<g:condition>new</g:condition>'."\n");
fwrite($fp, '<summary><![CDATA['.strip_tags($_product->getShortDescription()).']]></summary>'."\n");
fwrite($fp, '<id>'.$_product->getId().'</id>'."\n");
fwrite($fp, '<image_link><![CDATA['.Mage::getBaseUrl('media').'/catalog/product/'.$_product->getImage().']]></image_link>'."\n");
fwrite($fp, '<link href="'.$_product->getProductUrl().'"/>'."\n");
fwrite($fp, '<g:quantity>'.$qty.'</g:quantity>'."\n");
fwrite($fp, '<g:price>'.number_format($_priceIncludingTax, 2, ',').' EUR</g:price>'."\n");
fwrite($fp, '<g:gtin>'.$_product->getEan().'</g:gtin>'."\n");
fwrite($fp, '<g:google_product_category><![CDATA['.$cat.']]></g:google_product_category>'."\n");
fwrite($fp, '<g:availability>'.$stock.'</g:availability>'."\r\n");
fwrite($fp, '<g:shipping>'."\r\n");
fwrite($fp, '<g:country>DE</g:country>'."\r\n");
fwrite($fp, '<g:service>Standard</g:service>'."\r\n");
fwrite($fp, '<g:price>'.number_format($shipping, 2, ',').' EUR</g:price>'."\r\n");
fwrite($fp, '</g:shipping>'."\r\n");
fwrite($fp, '</entry>'."\n");
}
fwrite($fp, '</feed>'."\n");
fclose($fp);
@unlink($newFile);
rename($tmpFile, $newFile);
This Script Need the product attributes "EAN", "manufacturer"
and the category attribute (text): google_category
List of Google Categories:
German: http://www.google.com/basepages/producttype/taxonomy.de-DE.txt
USA: http://www.google.com/basepages/producttype/taxonomy.en-US.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment