Skip to content

Instantly share code, notes, and snippets.

@stnc
Last active December 20, 2017 15:20
Show Gist options
  • Save stnc/d34fe2a1bd82295d9d475574bab1a9cc to your computer and use it in GitHub Desktop.
Save stnc/d34fe2a1bd82295d9d475574bab1a9cc to your computer and use it in GitHub Desktop.
magento google merchant xml export
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set('memory_limit', '8000M');
$file_name = "xml_export_google1234.xml";
$google_category = 'Yiyecek, İçecekler ve Tütün Mamülleri > Yiyecek Çeşitleri > Meyve ve Sebzeler';
$mageFilename = dirname(realpath(__FILE__)) . '/../app/Mage.php';
require_once $mageFilename;
Mage::app();
$prod_model = Mage::getModel("catalog/product");
$cat_model = Mage::getModel("catalog/category");
$prod_helper = Mage::helper('catalog/output');
ini_set('memory_limit', '8000M');
//$conn = Mage::getSingleton('core/resource');
//$db = $conn->getConnection('core_write');
//http://blog.chapagain.com.np/magento-how-to-filter-product-collection-using-2-or-more-category-filters/
//https://fishpig.co.uk/magento/tutorials/addattributetofilter/
//https://nwdthemes.com/2014/10/14/getting-products-by-attribute-or-category-in-magento/
/*
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter()
->addLevelFilter(1)
->addOrderField('name');
foreach ($categories as $prod) {
$prod = $prod_model->reset()->load($prod->getId());
echo $prod->getId;
}
die;*/
/*
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinTable('catalog_category_product', 'product_id=entity_id', array('category_id' => 'category_id'), null, 'left')
->addAttributeToFilter('category_id', array('in' => 13))
->addAttributeToSelect('*');
*/
$collection = Mage::getResourceModel('catalog/product_collection')->distinct(true);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
// $collection->addAttributeToFilter('sku', array('eq'=>array('PAR-G041159')));
$xml_out =
'<?xml version="1.0" encoding="utf-8"?><channel xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:g="http://base.google.com/ns/1.0" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd">
<id>0</id>
<title><![CDATA[DoğalAVM / https://www.dogalavm.com]]></title>
<description><![CDATA[Doğal Avm - Organik Ürün Pazar Yeri | Alışverişin Yeni Doğası.]]></description>
<link>https://www.dogalavm.com/</link>
<lastBuildDate>' . date("d.m.Y H:i:s") . '</lastBuildDate>
';
$out = fopen($file_name, "w");
fwrite($out, $xml_out);
$total_cnt = $collection->count();
$uniuqProductSkus = array();
/*
foreach ($collection as $produ) {
$produ = $prod_model->reset()->load($produ->getId());
$produid[] = $produ->getSku();
}
print_r($produid);
die;*/
foreach ($collection as $prod) {
$prod = $prod_model->reset()->load($prod->getId());
// print_r(get_class_methods($prod));
//print_r($prod->getCategoryIds());
//echo ($prod->getCategoryIds()[0]) . "\n";
// diE;
if (!in_array( "164", $prod->getCategoryIds())) {
if (in_array($collection->getAllIds(), $uniuqProductSkus)) {
continue;
} else {
if ($prod->getTypeId() == 'configurable') {
// continue;
} // if sonu
// if ($prod->getSku() != 'PAR-G041159')
// {
// echo ".";
// continue;
// } // if sonu
$cnt++;
//echo "GOOGLE - " . $cnt . "/" . $total_cnt . " - " . $prod->getSku() . " - " . $prod->getName() . "\n";
/// Ek alanlar -----------------------------------------
if ($prod->getShortDescription() != "") {
$prod_desc = $prod->getShortDescription();
} // if sonu
else {
$prod_desc = $prod->getName();
} // else sonu
$prod_desc = clear_text($prod_desc);
$manufacturer = $prod->getAttributeText('manufacturer');
$base_image = str_replace_https(Mage::helper('catalog/image')->init($prod, 'image')->resize(500)->__toString());
/// Stok -----------------------------------------
$stock = $prod->getStockItem();
if ($stock->getIsInStock()) {
$availability = "in stock";
} else {
$availability = "out of stock";
}
/// Kategori -----------------------------------------
$cat_ids = $prod->getCategoryIds();
$cat_name = "";
$cat_id = "";
if (count($cat_ids) > 0) {
$cat_ids = array_reverse($cat_ids);
$category = $cat_model->load($cat_ids[0]);
$parents = explode("/", $category->getPath());
if (count($parents) > 1) {
unset($parents[0]);
unset($parents[1]);
$cat_names = array();
foreach ($parents as $pcat_id) {
if ($pcat_id > 2) {
$pcat = $cat_model->load($pcat_id);
$cat_names[] = $pcat->getName();
} // if sonu
} // foreach sonu
$cat_name = join(" > ", $cat_names);
} // if sonu
} // if sonu
/// XML -----------------------------------------
$price = '<g:price>' . number_format($prod->getFinalPrice(), 2, ",", "") . 'TL</g:price>';
$sale_price = '';
if ($prod->getFinalPrice() < $prod->getPrice()) {
$sale_price = '<g:sale_price>' . number_format($prod->getFinalPrice(), 2, ",", "") . 'TL</g:sale_price>';
} // if sonu
$xml_out =
'
<item>
<title><![CDATA[' . $prod->getName() . ']]></title>
<link><![CDATA[' . $prod->getProductUrl() . ']]></link>
<description><![CDATA[' . $prod_desc . ']]></description>
<g:id><![CDATA[' . $prod->getSku() . ']]></g:id>
<g:image_link><![CDATA[' . $base_image . ']]></g:image_link>
' . $price . '
' . $sale_price . '
<g:condition>new</g:condition>
<g:availability>' . $availability . '</g:availability>
<g:brand><![CDATA[' . $manufacturer . ']]></g:brand>
<g:product_type><![CDATA[' . $cat_name . ']]></g:product_type>
<g:google_product_category><![CDATA[' . $google_category . ']]></g:google_product_category>
<g:mobile_link/>
<g:shipping/>
</item>
';
}
fwrite($out, $xml_out);
// if ($cnt > 20)
// {
// break;
// } // if sonu
}
} // foreach sonu
$xml_out = '</channel>';
fwrite($out, $xml_out);
fclose($out);
######################################################################################################################################################################
######################################################################################################################################################################
######################################################################################################################################################################
// #######################################################################################################
function clear_text($text)
{
$pairs = array(
"\x0C" => "&#x0C;",
"\x15" => "&#x15;",
"\x26" => "&#x26;",
"\x5D" => "&#x5D;",
"\x03" => "&#x03;",
"\x05" => "&#x05;",
"\x0E" => "&#x0E;",
"\x16" => "&#x16;",
);
$text = strtr($text, $pairs);
return $text;
} // function sonu #######################################################################################
// #######################################################################################################
function str_replace_https($url)
{
return str_replace("https:", "http:", $url);
} // function sonu #######################################################################################
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment