We have a storelocator module on our Magento store, it works well but it is not being added to the Magento google sitemap.
Is there a way to add to our sitemap which Magento automatically generates?
You need to override the method Mage_Sitemap_Model_Sitemap::generateXml
because Magento does not offer an event you can use for that.
Insert your code after the cms pages are added
This is the code that adds the cms pages to the sitemap:
/**
* Generate cms pages sitemap
*/
$changefreq = (string)Mage::getStoreConfig('sitemap/page/changefreq', $storeId);
$priority = (string)Mage::getStoreConfig('sitemap/page/priority', $storeId);
$collection = Mage::getResourceModel('sitemap/cms_page')->getCollection($storeId);
foreach ($collection as $item) {
$xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
htmlspecialchars($baseUrl . $item->getUrl()),
$date,
$changefreq,
$priority
);
$io->streamWrite($xml);
}
unset($collection);
You have to do a similar thing with your page.
You need to loop through your custom pages and do this:
$xml = sprintf('<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
htmlspecialchars($baseUrl . $item->getUrl()), //url of your custom page
$date,
$changefreq, //should be a config value
$priority //should be a config value
);
$io->streamWrite($xml); //write xml node to the big sitemap xml
Yes you certainly can add to the sitemap, and you should preferably do this in your own module (with a rewrite), or at the very least by moving the relevant class to the local
branch (but this is not a great idea - much less upgrade safe than a module).
The class in question is Mage_Sitemap_Model_Sitemap
and the method is generateXml()
there is pretty straight forward logic which pulls the category, product and cms page collections, iterates through each item and then adds the information about each item to the xml string. The categories logic looks something like this:
$changefreq = (string)Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
$priority = (string)Mage::getStoreConfig('sitemap/category/priority', $storeId);
$collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
foreach ($collection as $item) {
$xml = sprintf(
'<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
htmlspecialchars($baseUrl . $item->getUrl()),
$date,
$changefreq,
$priority
);
$io->streamWrite($xml);
}
unset($collection);
All you need is a collection with a URL retrievable from each item in the collection and you can add another method exactly as above. Note that $date
is set higher up the method and $changefreq
and $priority
are set just before the collection is built.