Magento – Can (and if so how) one modify the RSS feeds in order to include additional data without extensions

ce-1.9.0.1google-product-feedsmagento-1.9rss

I'm currently working with a Magento 1.9.x Community Edition instance, trying to get an AvantLink metric tracking system to work properly. Most of the work has been done; they're seeing the calls to their tracking script from the success.phtml page with no issues. The problem is that they also need access to an RSS feed (they prefer Google Shopping format, I guess, but said that the standard RSS as is built in to Magento would be fine as well [legacy?]), and that can provide data on our products.

After making certain that all of the RSS capabilities [built in] were enabled, I gave them the URL, and they were able to successfully pull the feed. Unfortunately, while the feed gives them some of what they need, they are still missing the following vital bits of information:

  • id
  • product_type
  • image_link
  • price
  • sale_price

I've looked everywhere that I can in the admin page, and I've done plenty of google searching, with my (admittedly limited) google-fu. I've been able to turn up plenty of for-pay extensions that'll export a Google Shopping RSS feed, or other RSS feeds, that can be customized to this extent, I believe, but I'm not going to be able to sell my clients on a non-free extension to get this working.

Does anybody know if there is a way that I can add these fields to the RSS feeds, via the admin control page? If not, can anybody direct me to the appropriate locations, in code, where I might be able to code these additions myself? As always, any helpful tidbits, or even pointers in vaguely correct directions, are greatly appreciated.

Thanks in advance. Please feel free to let me know, if I've left out any information that is necessary for figuring this out.

-Damon

Best Answer

The blocks used in Rss creation are in app/code/core/Mage/Rss/Block

Just a guess, but maybe you want to modify the app/code/core/Mage/Rss/Block/Catalog/Category.php file? There are several, take a look at the blocks and see which affects the feed you are concerned with.

I would create a simple module that rewrites the block you need to modify. A quick example, assuming that your company is called Yourcompany :

app/code/local/Yourcompany/Rss/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Yourcompany_Rss>
            <version>0.1.0</version>
        </Yourcompany_Rss>
    </modules>
    <global>
        <blocks>
            <yourcompany_rss>
                <class>Yourcompany_Rss_Block</class>
            </yourcompany_rss>
            <rss>
                <rewrite>
                    <catalog_category>Yourcompany_Rss_Block_Rss_Catalog_Category</catalog_category>
                </rewrite>
            </rss>
        </blocks>
        <helpers>
            <yourcompany_rss>
                <class>Yourcompany_Rss_Helper</class>
            </yourcompany_rss>
        </helpers>
    </global>
</config>

app/code/local/Yourcompany/Rss/Block/Rss/Catalog/Category.php

class Yourcompany_Rss_Block_Rss_Catalog_Category extends Mage_Rss_Block_Catalog_Category {

protected function _toHtml()
{
    $categoryId = $this->getRequest()->getParam('cid');
    $storeId = $this->_getStoreId();
    $rssObj = Mage::getModel('rss/rss');
    if ($categoryId) {
        $category = Mage::getModel('catalog/category')->load($categoryId);
        if ($category && $category->getId()) {
            $layer = Mage::getSingleton('catalog/layer')->setStore($storeId);
            //want to load all products no matter anchor or not
            $category->setIsAnchor(true);
            $newurl = $category->getUrl();
            $title = $category->getName();
            $data = array('title' => $title,
                'description' => $title,
                'link'        => $newurl,
                'charset'     => 'UTF-8',
            );

            $rssObj->_addHeader($data);

            $_collection = $category->getCollection();
            $_collection->addAttributeToSelect('url_key')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('is_anchor')
                ->addAttributeToFilter('is_active',1)
                ->addIdFilter($category->getChildren())
                ->load()
            ;
            $productCollection = Mage::getModel('catalog/product')->getCollection();

            $currentCategory = $layer->setCurrentCategory($category);
            $layer->prepareProductCollection($productCollection);
            $productCollection->addCountToCategories($_collection);

            $category->getProductCollection()->setStoreId($storeId);
            /*
            only load latest 50 products
            */
            $_productCollection = $currentCategory
                ->getProductCollection()
                ->addAttributeToSort('updated_at','desc')
                ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
                ->setCurPage(1)
                ->setPageSize(50)
            ;

            if ($_productCollection->getSize()>0) {
                $args = array('rssObj' => $rssObj);
                foreach ($_productCollection as $_product) {
                    $args['product'] = $_product;
                    $this->addNewItemXmlCallback($args);
                }
            }
        }
    }
    return $rssObj->createRssXml();
}

}

In this case, if you modify the collection at ~ line 47

$_productCollection = $currentCategory
                    ->getProductCollection()

to include the attributes you need you should have them in the feed.

You will also need your module declared in app/etc/modules, but your question was about rss, not custom modules ; )

So, take a look at the contents of app/code/core/Mage/Rss/Block/ , and rewrite the file that corresponds to your feed.

** I used a module name of Rss, that was totally arbitrary, you can name the module anything you want.

Related Topic