Sitemap mit Bildern Titel Bild

How do you create image sitemaps in a Magento online shop (multi-shop with 2 websites and 3 languages / store views per website) and exclude certain page types for individual store views? Here is one possible solution: First, we create subfolders for each sitemap, e.g. /sitemaps/sitemap-websitename1-storeviewname1/,  /sitemaps/sitemap-websitename1-storeviewname2/, etc. We then create a sitemap for each store view in the Magento backend and generate it, so that we have one sitemap per sitemap folder as a result (/sitemaps/sitemap-websitename1-storeviewname1/sitemap.xml,  /sitemaps/sitemap-websitename1-storeviewname2/sitemap.xml, etc.).

 

 So that search engine crawlers know which sitemap is intended for which shop, we now create 2 robots.txt files (one for each website, e.g. robots.website1.txt, robots.website2.txt) with the following content:

 

Sitemap: https://website1/sitemaps/sitemap-websitename1-storeviewname1/sitemap.xml

Sitemap: https://website1/sitemaps/sitemap-websitename1-storeviewname2/sitemap.xml

Sitemap: https://website1/sitemaps/sitemap-websitename1-storeviewname3/sitemap.xml

 

or

 

Sitemap: https://website2/sitemaps/sitemap-websitename2-storeviewname1/sitemap.xml

Sitemap: https://website2/sitemaps/sitemap-websitename2-storeviewname2/sitemap.xml

Sitemap: https://website2/sitemaps/sitemap-websitename2-storeviewname3/sitemap.xml

 

We then reference these two robots files in the .htaccess:

 

RewriteCond %{HTTP_HOST} ^.*?website1\.com$ [NC]

RewriteRule ^robots\.txt$ robots.website1.txt

 

RewriteCond %{HTTP_HOST} ^.*?website2\.de$ [NC]

RewriteRule ^robots\.txt$ robots.website2.txt

 

Adjust the TLD (.de / .com) as required, of course! We can then automate sitemap generation under System->Configuration->CATALOG->Catalog in the Magento backend and are essentially finished, but we still do not have any images in the sitemap or any exclusion groups. We will address these in the following steps:

 

To do this, we create an extension (namespace and extension name can be chosen freely) under 

 

/app/code/local/Econcess/Sitemap/

 

, which we then register using

 

/app/etc/modules/Econcess_Sitemap.xml

 

and activate with the following content:

 

<?xml version="1.0"?>

<config>

    <modules>

        <Econcess_Sitemap>

            <active>true</active>

            <codePool>local</codePool>

        </Econcess_Sitemap>

    </modules>

</config>

 

Die Extension selbst besteht dann nur aus 3 Dateien:

 

1) /app/code/local/Econcess/Sitemap/etc/config.xml

2) /app/code/local/Econcess/Sitemap/Model/Sitemap.php

3) /app/code/local/Econcess/Sitemap/Model/Resource/Catalog/Product.php

 

In der /app/code/local/Econcess/Sitemap/etc/config.xml geben wir bekannt, welche Models wir überschreiben möchten (um die Bilddaten zu generieren und Änderungen an der Produktauswahl vorzunehmen):

 

<?xml version="1.0"?>

<config>

    <modules>

        <Econcess_Sitemap>

            <version>0.0.1</version>

        </Econcess_Sitemap>

    </modules>

    <global>

                    <models>

            <Econcess_Sitemap>

                <class>Econcess_Sitemap_Model</class>

            </Econcess_Sitemap>                                                                   

                                               <sitemap>

                                                               <rewrite>

                                                                               <sitemap>Econcess_Sitemap_Model_Sitemap</sitemap>

                                                               </rewrite>

                                               </sitemap>

                                               <sitemap_resource>

                                                               <rewrite>

                                                                               <catalog_product>Econcess_Sitemap_Model_Resource_Catalog_Product</catalog_product>

                                                               </rewrite>

                                               </sitemap_resource>

        </models>

    </global>

</config>

 

We define how the images (and CMS pages) are then generated in /app/code/local/Econcess/Sitemap/Model/Sitemap.php. The basis for this file in our project was the source code from the Magento Core Team. We extended it so that an array containing the product IDs is generated according to various criteria (attribute_set_id and muster_art), and the already prepared product collection is then "thinned out":

 

$collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);

                              

                               $excludeIds = Mage::getModel('catalog/product')

                                               ->getCollection()

                                               ->addAttributeToFilter(

                                               array(

                                               array('attribute'=> 'attribute_set_id','eq' => 'xxx'), 

                                               )

                                               )

                                               ->addAttributeToFilter(

                                               array(

                                               array('attribute'=> 'muster_art','neq' => 'yyy'),

                                               )

                                               )

                                               ->addAttributeToFilter(

                                               array(

                                               array('attribute'=> 'muster_art','neq' => 'zzz'),   

                                               )

                                               )

                                               ->getAllIds();

                               foreach ($excludeIds as $id) {

                                               unset($collection[$id]);

                               }

 

To obtain the required image information, we also need to extend the product resource as mentioned above, in /app/code/local/Econcess/Sitemap/Model/Resource/Catalog/Product.php (source here as well: Magento Core Team), with the simple content:

 

class Econcess_Sitemap_Model_Resource_Catalog_Product extends Mage_Sitemap_Model_Resource_Catalog_Product

{

  protected function _prepareProduct(array $productRow)

  {

    $product = new Varien_Object();

    $product->setId($productRow[$this->getIdFieldName()]);

    $id = $product->getId();

    $productMedia = Mage::getModel('catalog/product')->load($id)->getImage();

    $product->setMedia($productMedia);

    $productName = Mage::getModel('catalog/product')->load($id)->getName();

    $product->setName($productName);

    $productUrl = !empty($productRow['url']) ? $productRow['url']: 'catalog/product/view/id/' . $product->getId();

    $product->setUrl($productUrl);

    return $product;

  }

}

 

With this, everything is generated perfectly and we are done! Tested with Magento 1.9.2.3.