I've updated the code for the google sitemaps feed to use the DOM document, which makes it a lot nicer code wise, plus allows the auto formatting as well to make it visually better when trying to read it. I've fully documented all of my code (something I hope OC will have as standard in the future as it is lacking it quite badly currently IMO)
To install this, simply upload the code below to
Code: Select all
/catalog/controller/feed/google_sitemap_improved.php
http://www.yourstore.com/[u]store_direc ... pplicable/[/u]index.php?route=feed/google_sitemap_improved
Code: Select all
<?php
/**
* @author Jay Gilford - http://www.jaygilford.com/
*/
class ControllerFeedGoogleSitemapImproved extends Controller
{
// Holds the DOM Document object
private $dom;
// Holds the urlset root element for the DOM
private $root;
/**
* ControllerFeedGoogleSitemapImproved::index()
* action for controller
*
* @return void
*/
public function index()
{
// Load all models
$this->load->model('tool/seo_url');
$this->load->model('catalog/product');
$this->load->model('catalog/category');
$this->load->model('catalog/manufacturer');
$this->load->model('catalog/information');
// Create new dom docuemnt and root and assign them to the private vars
$this->dom = new DOMDocument('1.0', 'UTF-8');
$this->root = $this->dom->createElement('urlset');
$this->root->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$this->root = $this->dom->appendChild($this->root);
// Add main domain
$this->addUrl(HTTP_SERVER, '1.0', 'daily');
// Products //
$products = $this->model_catalog_product->getProducts();
foreach ($products as $product) {
$this->addUrl(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $product['product_id'],
'0.9');
}
// Manufacturers //
$manufacturers = $this->model_catalog_manufacturer->getManufacturers();
foreach ($manufacturers as $manufacturer) {
$this->addUrl(HTTP_SERVER .
'index.php?route=product/manufacturer&manufacturer_id=' . $manufacturer['manufacturer_id'],
'0.7');
}
// Informations //
$informations = $this->model_catalog_information->getInformations();
foreach ($informations as $information) {
$this->addUrl(HTTP_SERVER .
'index.php?route=information/information&information_id=' . $information['information_id'],
'0.5');
}
// Add Special, Contact, Sitemap, Search, Login and account URL's
$this->addUrl(HTTP_SERVER . 'index.php?route=information/contact', '0.5');
$this->addUrl(HTTP_SERVER . 'index.php?route=information/sitemap', '0.5');
$this->addUrl(HTTP_SERVER . 'index.php?route=product/special', '0.5');
$this->addUrl(HTTP_SERVER . 'index.php?route=product/search', '0.5');
$this->addUrl(HTTP_SERVER . 'index.php?route=account/account', '0.5');
$this->addUrl(HTTP_SERVER . 'index.php?route=account/login', '0.5');
// Categories //
$this->getCategories(0);
$this->dom->formatOutput = true;
$this->response->addHeader('Content-Type: application/xml');
$this->response->setOutput($this->dom->saveXML());
}
/**
* ControllerFeedGoogleSitemapImproved::addUrl()
* method to add urls to sitemap
*
* @param string $url
* @param string $priority
* @param string $frequency
* @return void
*/
protected function addUrl($url, $priority, $frequency = 'weekly')
{
// Add elements
$u = $this->dom->createElement('url');
$u->appendChild($this->dom->createElement('loc', htmlentities((string )$this->
model_tool_seo_url->rewrite($url))));
$u->appendChild($this->dom->createElement('priority', $priority));
$u->appendChild($this->dom->createElement('changefreq', htmlentities($frequency)));
// Add to urlset
$this->root->appendChild($u);
}
/**
* ControllerFeedGoogleSitemapImproved::getCategories()
* Adds the list of categories to the sitemap recursively
*
* @param integer $parent_id
* @param string $current_path
* @return void
*/
protected function getCategories($parent_id, $current_path = '')
{
// Load categories
$results = $this->model_catalog_category->getCategories($parent_id);
// Loop through each category, increasing the path as the structure gets deeper
foreach ($results as $result) {
if (!$current_path) {
$new_path = $result['category_id'];
} else {
$new_path = $current_path . '_' . $result['category_id'];
}
// add category url
$this->addUrl(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path,
'0.7');
// Recursion to load new category level
$this->getCategories($result['category_id'], $new_path);
}
}
}
Notice that I've not done the same product X times, only once, since I don't think having multiple links to duplicate content is really beneficial (SEO guru's may disagree)
Not sure how far back this will work but should be fine for 1.4.7 +
Cheers
Jay Gilford
http://www.jaygilford.com/