14

I need to make sure a handful of products are NOT in the generated Sitemap.xml file in Magento. I found a couple of example for things like CMS Pages but nothing for doing product pages, and more then 1 product. I am brand new to Magento

Anyone know how this can be done?

3 Answers 3

7

Out of the box, no, there's no way to exclude certain products from the sitemap generated by Magento's Catalog -> Google Sitemap feature.

If I was going to go about doing this programmatically, modern versions of Magento (checked in the 1.7.x branch, this might be around in earlier/EE versions) use the following resource model class

Mage_Sitemap_Model_Resource_Catalog_Product

to fetch a list of products.

#File: app/code/core/Mage/Sitemap/Model/Sitemap.php
$collection = Mage::getResourceModel('sitemap/catalog_product')->getCollection($storeId);

This is not a standard Magento CRUD model, and getCollection does not return a collection object. Instead, getCollection manually queries the database for these products.

If I was going to implement functionality that prevented certain products from showing up in the site map, I'd try to either

  1. A class rewrite the getCollection method which calls the parent::getCollection, and then manually filters out any products from the array

  2. A class rewrite on _addFilter which calls the parent::_addFilter method, and then adds an additional WHERE clause(s) to the _select to exclude the specific product(s). Sort of a hack, but it's the only method where you have access to the _select object used to query the database. Ideally you'd want to have some sort of global/static flag so you only added your new WHERE clause(s) once.

9

Since Magento 1.9.0. you can do this without touching any core file.

There are two new events you can observe:

sitemap_categories_generating_before
sitemap_products_generating_before

To exclude products based on attribute, you can do this:

Event lookup

Add an observer to sitemap_products_generating_before

app\code\community\My\Module\etc\config.xml
<events>
    <sitemap_products_generating_before>
        <observers>
            <my_module>
                <class>my_module/observer</class>
                <method>excludeProductsFromSitemap</method>
            </my_module>
        </observers>
    </sitemap_products_generating_before>
</events>

Event execution

app\code\community\My\Module\Model\Observer.php
public function excludeProductsFromSitemap(Varien_Event_Observer $observer)
{
   $collection = $observer->getCollection();
   $items = $collection->getItems();

   $excludeIds = Mage::getModel('catalog/product')
       ->getCollection()
       ->setStoreId($observer->getStoreId()) # requieres Magento 1.9.3.0
       ->addAttributeToFilter('use_in_sitemap', 0)
       ->getAllIds();

   foreach ($excludeIds as $id) {
       unset($items[$id]);
   }
   
   $collection->setItems($items);
}

Add attribute

  1. add a product attribute yes/no named use_in_sitemap (maybe default value yes)
  2. add this attribute to all attribute sets
  3. set the products you want to exclude to no

Note: until Magento 1.9.3.0 the attribute should be set to global scope.

1

You can disable those products or set the visibility field to 'Not visible individually'. In the sitemap only enabled and visible products are listed.
If you need those products enabled, just disable them, build sitemap.xml and enable them again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.