Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Collections for products, categories, and pages. Filter them, use any custom PHP you like and create an XML file. Couldn't find it so I built it. Very useful for carts that only have configurable "main" products and don't want all the bits and pieces "simple products" showing in the sitemap. 1. create a my_sitemap.xml to be overwritten. 2. K…
<?php
/* Compilation includes configuration file */
define('MAGENTO_ROOT', getcwd());
$compilerConfig = MAGENTO_ROOT . '/includes/config.php';
if (file_exists($compilerConfig)) {
include $compilerConfig;
}
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::app()->getRequest()->getRouteName();
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
/* EOF Magento Config */
// SET YOUR BASE URL
$baseUrl = "http://www..com/"; // dont forget trailing /
// GET ALL
$products = Mage::getModel('catalog/product')->getCollection();
//var_dump($products->getFirstItem()->getData()); // dump just the first for testing
$products->addFieldToFilter('type_id','configurable'); // SHOW ME ONLY CONFIGURABLE PRODUCTS
$products = $products->getData();
$categories = Mage::getModel('catalog/category')->getCollection();
$categories = $categories->getData();
$pages = Mage::getModel('cms/page')->getCollection();
$pages->addFieldToFilter('is_active','1'); // they gotta be active
$pages = $pages->getData();
//START OUR XML CODE
$fp = fopen('my_sitemap.xml', 'w');
fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
");
//WRITE ACTIVE PAGES
foreach($pages as $page){
if($page["identifier"]=="home"){
//skip we dont need /home -- enter any of page name above to exclude
} else {
fwrite($fp, "
<url><loc>".$baseUrl.$page["identifier"]."</loc></url>
<lastmod>".$page["updated_time"]."</lastmod>
<priority>0.5</priority>
");
}
}
//WRITE ACTIVE CATEGORIES
foreach($categories as $cat){
if($cat["parent_id"]==0 OR $cat["parent_id"]==1){
//skip b/c this is the root and the "Default Category"
} else {
$realCats = $read->fetchRow("SELECT * FROM catalog_category_flat_store_1 WHERE entity_id=".$cat["entity_id"]." AND is_active=1");
fwrite($fp, "
<url><loc>".$baseUrl.$realCats["url_path"]."</loc></url>
<lastmod>".$realCats["updated_at"]."</lastmod>
<priority>0.7</priority>
");
}
}
//WRITE CATEGORIES
foreach($products as $item){
//echo $item["sku"]; // check it works echo the sku
$result = $read->fetchRow("SELECT * FROM catalog_product_flat_1 WHERE sku='".$item["sku"]."'");
//checkout results
//echo "<pre>";
//print_r($result);
//break; // just do one loop
if($result){
//lets write the URLs
fwrite($fp, "
<url><loc>".$baseUrl.$result["url_path"]."</loc></url>
<lastmod>".$result["updated_at"]."</lastmod>
<priority>0.8</priority>
");
} else {
//do nothing b/c we had an error
}
unset($result);
}
fwrite($fp, "</urlset>");
fclose($fp);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.