Creating and Customizing Google Sitemaps with Magento

You can create an XML sitemap for you Magento store by logging into your administration panel and going to Catalog > Google Sitemap.

This interface is pretty straightforward, just be aware that each store must have its own sitemap. Also note that the the Path must be writable by the file system.

When you click “Save & Generate” a new xml sitemap will be generated. The frequency and priority are set using default settings but can be changed in System > Configuration > Catalog: Google Sitemap. This is also where you will set up your sitemaps to be automatically generated by the Magento cron job.

The Magento sitemap only includes catalog categories, products, and CMS pages by default. Lets discuss how to customize what is included in the sitemaps.

Customizing Magento’s XML Sitemap

The generateXml() function in the Mage_Sitemap_Model_Sitemap class is where all of the magic happens. We are going to overwrite this function. I’m not going to get into extending and overwriting Magento core here, if you need to learn about it you can read my post about it.

The generateXml() function has a lot going on, so to make it easier I just copy the entire function into my new class and never refer to the original function.

I recommend putting your custom code near the end of the function just after the last unset($collection); in the “Generate cms pages sitemap” section. I also recommend copying the same structure that the other sections use.

Here is what my custom code looks like with comments:

/**
 * Generate customurls sitemap
 */
// reuse same settings as the cms page section, you can create custom ones if you know how
$changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq', $storeId);
$priority   = (string)Mage::getStoreConfig('sitemap/page/priority', $storeId);

// you can do a lot of things to get these urls, this is a simplified example
$collection = array(
    'module1/page1',
    'module1/page2',
    'module2/page1'
);

// this is the most important part, it writes the data to the xml file
foreach ($collection as $item) {
    $xml = sprintf(
        '%s%s%s%.1f',
        // the core function concatenates $baseUrl with the url,
        // you can also use ->getUrl() or some other method to get the url
        htmlspecialchars($baseUrl . $item),
        $date,
        $changefreq,
        $priority
    );
    $io->streamWrite($xml);
}
unset($collection);

This is a very basic example. There are many ways to customize this, but this will get you started.

Go back to Catalog > Google Sitemap and click “Save & Generate” and then go to your sitemap url to see if the new urls are there.