1

I want to include category path for products inside my sitemap.xml.

Currently I am using Atwix Sitemap module, to split sitemap in multiple sitemaps. See: https://github.com/Atwix/Sitemap

How can I edit this module or the default Magento code, to include full product url's?

1

To include full category path, you can create a small extension and try this:

Event

<sitemap_products_generating_before>
    <observers>
        <product_sitemap>
            <class>[module]/observer</class>
            <method>extendProductXmlSitemap</method>
       </product_sitemap>
    </observers>
</sitemap_products_generating_before>

Observer

public function extendProductXmlSitemap(Varien_Event_Observer $observer)
{
    $collection = $observer->getCollection();
    $items      = $collection->getItems();
    $pathes     = array();

    $categoryIds = Mage::getResourceModel('sitemap/catalog_category')->getCollection($observer->getStoreId());
    $categoryIds = array_keys($categoryIds);
    $categories  = Mage::getResourceModel('catalog/category_collection')
        ->addIdFilter($categoryIds);

    foreach ($categories as $category) {
        $productsIds = $category->getProductCollection()->getAllIds();
        $func = function($categoryId, $productsId) {
            return sprintf('catalog/product/view/id/%d/category/%d', $productsId, $categoryId);
        };
        $pathes[] = array_map($func, array_fill(0, count($productsIds), $category->getId()), $productsIds);
    }

    $rewrites = Mage::getResourceModel('core/url_rewrite_collection')
        ->addFieldToFilter('target_path', array('in' => $pathes));

    foreach ($rewrites as $rewrite) {
        $item = new Varien_Object;
        $item->setId($rewrite->getIdPath());
        $item->setUrl($rewrite->getRequestPath());
        $items[$itemId] = $item;
    }

    $collection->setItems($items);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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