Post by JAY6390 » Fri Jun 11, 2010 9:41 am

Hey all,
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
then run
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);
        }
    }
}
 
If anyone has any suggestions as to what else I need to include or change please let me know
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/

Image


User avatar
Guru Member

Posts

Joined
Wed May 26, 2010 11:47 pm
Location - United Kingdom

Post by OC2PS » Sun Jul 25, 2010 6:02 am

Somehow not working for me. Google Webmaster Tools rejects it.

http://www.hennalap.com/index.php?route ... p_improved

When I visit the link, it doesnt appear as well formatted XML

OC2PS
OC 3.0.3.7, vQmod 2.6.2, Journal3 theme
Arcfesték, Csillámtetoválás, Henna
Image
Check out: All my extensions | My FREE extensions


User avatar
Active Member

Posts

Joined
Wed Jul 22, 2009 4:15 am
Location - Hungary

Post by JAY6390 » Sun Jul 25, 2010 7:10 am

Thats because your seo url rewriter is causing an error for some reason

Image


User avatar
Guru Member

Posts

Joined
Wed May 26, 2010 11:47 pm
Location - United Kingdom

Post by OC2PS » Sun Jul 25, 2010 4:59 pm

But I am using OC's standard SEO URL rewriter!

Could it be that the Sitemap feature is not equipped to deal with "special characters"? My SEO keywords, and thus URLs contain letters from the Hungarian alphabet. While this works fine for URLs, could this be causing mischief when it comes to sitemap?

If so, how can we make sure that sitemap is capable of reading and relaying these special characters? Simply converting them into English characters (e.g. é to e, á to a, ó to o etc) won't work because the actual URLs do have these characters, and from SEO perspective, it is important for me that the URLs keep having them.

OC2PS
OC 3.0.3.7, vQmod 2.6.2, Journal3 theme
Arcfesték, Csillámtetoválás, Henna
Image
Check out: All my extensions | My FREE extensions


User avatar
Active Member

Posts

Joined
Wed Jul 22, 2009 4:15 am
Location - Hungary

Post by OC2PS » Sun Jul 25, 2010 5:11 pm

P.S. The original sitemap in my 1.2.9 seems to work fine.
But neither the original, nor this improved version are ok in my 1.4.8b

OC2PS
OC 3.0.3.7, vQmod 2.6.2, Journal3 theme
Arcfesték, Csillámtetoválás, Henna
Image
Check out: All my extensions | My FREE extensions


User avatar
Active Member

Posts

Joined
Wed Jul 22, 2009 4:15 am
Location - Hungary

Post by fraveton » Wed Apr 06, 2011 11:07 am

Hi Jay,

I have the same problem when using OC's SEF integrated feature.
Google returns an error message at line 10.
Without SEF urls, it works, even though I find the format quite unusual for Google to "digest".
Anyway, non-SEF is not an option for any site now so I would really appreciate your help here too.
(I am using OC 1.4.9.1)

F.

New member

Posts

Joined
Wed Apr 06, 2011 11:00 am

Post by JAY6390 » Wed Apr 06, 2011 5:42 pm

Hi - Sorry not sure what you are wanting to do here tbh

Image


User avatar
Guru Member

Posts

Joined
Wed May 26, 2010 11:47 pm
Location - United Kingdom

Post by fraveton » Thu Apr 07, 2011 6:13 pm

Hi Jay,

I finally managed to find that the error was coming from the .htaccess so problem is solved.
All seems to work fine now.
Great extension!

New member

Posts

Joined
Wed Apr 06, 2011 11:00 am

Post by richard211986 » Fri Apr 15, 2011 11:55 pm

i have the same error appear for the seo url path, any suggestions on what i have to change to get it to validate?

Code: Select all

Invalid at the top level of the document. Error processing resource 'http://www.swiftsupplements.co.uk/index.php?route=feed...

<b>Notice</b>: Undefined index:  query in <b>/home/www/swiftsupplements.co.uk/catalog/model/tool/seo_url.p...
thanks
Rich

Active Member

Posts

Joined
Sun Sep 20, 2009 5:34 am
Who is online

Users browsing this forum: No registered users and 2 guests