Magento adding you own custom RSS Feeds

You know you will need, before or after, to add your own custom RSS feeds to your Magento.
For example, I needed to create some custom RSS Feeds to list bestseller products by date (bestseller weekly, monthly, etc…)
How to do it? It’s quite complex but not so much if you follow this mini tutorial:

First of all, you need to setup a new namespace directory under your “local” dir, this will overwrite your current RSS Mage methods but not all.

So, create your directory app/local/Simplicissimus/Rss. (change Simplicissimus with your own Namespace and change it everywhere)

Create inside it the following directories:

  • Block
  • Block/Catalog
  • controllers
  • etc

Starting with the configuration XML, you need to create a file etc/config.xml like this:

<?xml version="1.0"?>
<config>
<modules>
<Simplicissimus_Rss>
<version>0.1.0</version>
</Simplicissimus_Rss>
</modules>
<global>
<blocks>
<rss>
<rewrite>
<list>Simplicissimus_Rss_Block_List</list>
<catalog_category>Simplicissimus_Rss_Block_Catalog_Category</catalog_category>
<catalog_bestseller>Simplicissimus_Rss_Block_Catalog_Bestseller</catalog_bestseller>
<catalog_bestsellerweekly>Simplicissimus_Rss_Block_Catalog_Bestsellerweekly</catalog_bestsellerweekly>
<catalog_bestsellermonthly>Simplicissimus_Rss_Block_Catalog_Bestsellermonthly</catalog_bestsellermonthly>
<catalog_bestsellernarcissus>Simplicissimus_Rss_Block_Catalog_Bestsellernarcissus</catalog_bestsellernarcissus>
<catalog_worstseller>Simplicissimus_Rss_Block_Catalog_Worstseller</catalog_worstseller>
</rewrite>
</rss>
</blocks>
</global>
<frontend>
<routers>
<rss>
<use>standard</use>
<args>
<modules>
<Simplicissimus_Rss before="Mage_Rss">Simplicissimus_Rss</Simplicissimus_Rss>
</modules>
</args>
</rss>
</routers>
</frontend>
</config>

Now let’s go create the controller for new RSS urls, create a file called controllers/CatalogController.php

<?php
// Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Rss/controllers/CatalogController.php';
class Simplicissimus_Rss_CatalogController extends Mage_Rss_CatalogController
{
public function bestsellerAction()
{
$this->checkFeedEnable('bestseller');
$this->loadLayout(false);
$this->renderLayout();
}

public function bestsellerweeklyAction()
{
$this->checkFeedEnable('bestsellerweekly');
$this->loadLayout(false);
$this->renderLayout();
}

public function bestsellermonthlyAction()
{
$this->checkFeedEnable('bestsellermonthly');
$this->loadLayout(false);
$this->renderLayout();
}

public function bestsellernarcissusAction()
{
$this->checkFeedEnable('bestsellernarcissus');
$this->loadLayout(false);
$this->renderLayout();
}

public function worstsellerAction()
{
$this->checkFeedEnable('worstseller');
$this->loadLayout(false);
$this->renderLayout();
}
}

Now we need blocks, first of all create a file Block/List.php:

<?php
class Simplicissimus_Rss_Block_List extends Mage_Rss_Block_List
{

public function getRssMiscFeeds()
{
$this->resetRssFeed();
//$this->NewProductRssFeed();
//$this->SpecialProductRssFeed();
$this->SalesRuleProductRssFeed();

// SBF
$this->BestsellerRssFeed();
$this->BestsellerWeeklyRssFeed();
$this->BestsellerMonthlyRssFeed();
$this->BestsellerNarcissusRssFeed();
$this->WorstsellerRssFeed();

return $this->getRssFeeds();
}

public function BestsellerRssFeed()
{
$path = self::XML_PATH_RSS_METHODS.'/catalog/bestseller';
if((bool)Mage::getStoreConfig($path)){
$this->addRssFeed($path, $this->__('Ebook Bestseller (All time)'));
}
}

public function BestsellerWeeklyRssFeed()
{
$path = self::XML_PATH_RSS_METHODS.'/catalog/bestsellerweekly';
if((bool)Mage::getStoreConfig($path)){
$this->addRssFeed($path, $this->__('Ebook Bestseller (Ultima settimana)'));
}
}

public function BestsellerMonthlyRssFeed()
{
$path = self::XML_PATH_RSS_METHODS.'/catalog/bestsellermonthly';
if((bool)Mage::getStoreConfig($path)){
$this->addRssFeed($path, $this->__('Ebook Bestseller (Ultimo mese)'));
}
}

public function BestsellerNarcissusRssFeed()
{
$path = self::XML_PATH_RSS_METHODS.'/catalog/bestsellernarcissus';
if((bool)Mage::getStoreConfig($path)){
$this->addRssFeed($path, $this->__('Ebook Bestseller (Narcissus Self Published)'));
}
}

public function WorstsellerRssFeed()
{
$path = self::XML_PATH_RSS_METHODS.'/catalog/worstseller';
if((bool)Mage::getStoreConfig($path)){
$this->addRssFeed($path, $this->__('Ebook meno venduti'));
}
}

}

Well done, now we need to create one block file for each new RSS Feed we wanna publish, this file will be the one with the collection inside. Here I put only one of this for example you should need to complete the others or to remove from the other files:

<?php
class Simplicissimus_Rss_Block_Catalog_Bestseller extends Mage_Rss_Block_Abstract
{

/**
* Cache tag constant for feed reviews
*
* @var string
*/
const CACHE_TAG = 'block_html_rss_catalog_bestseller';

protected function _construct()
{
$this->setCacheTags(array(self::CACHE_TAG));
/*
* setting cache to save the rss for 10 minutes
*/
$this->setCacheKey('rss_catalog_bestseller');
$this->setCacheLifetime(600);
}

protected function _toHtml()
{
$newurl = Mage::getUrl('rss/catalog/bestseller');
$title = Mage::helper('rss')->__('Ebook Bestseller (All time)');

$rssObj = Mage::getModel('rss/rss');
$data = array('title' => $title,
'description' => $title,
'link'        => $newurl,
'charset'     => 'UTF-8',
);
$rssObj->_addHeader($data);

$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addOrderedQty()
->addFieldToFilter('type_id',  array('like'=>'downloadable'))
->addAttributeToFilter('status', 1)
->setOrder('ordered_qty', 'desc')
->setCurPage(1)
->setPageSize(20);

if ($_productCollection) {
$args = array('rssObj' => $rssObj);
foreach ($_productCollection as $_product) {
$args['product'] = $_product;
$this->addBestsellerXmlCallback($args);
}
}

return $rssObj->createRssXml();
}

public function addBestsellerXmlCallback($args)
{
$product = Mage::getModel('catalog/product')->load($args['product']['entity_id']);

Mage::dispatchEvent('rss_catalog_category_xml_callback', $args);

if ( $product->getTypeId() == "downloadable" )
$extendedDescr = "<p><strong>Autore: </strong>" . $product->getStealth_authors() . "<br /><strong>Editore: </strong>" . $product->getStealth_publisher() . "<br /><strong>Protezione: </strong>" . $product->getStealth_drm();
else
$extendedDescr = "<p>" . $product->getShortDescription() . "</p>";

//Mage::log($product->getData());

$description = '<table><tr>'
. '<td><a href="'.$product->getProductUrl().'"><img src="'
. $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75, 75)
. '" border="0" align="left" height="75" width="75"></a></td>'
. '<td  style="text-decoration:none;">' . $extendedDescr;

$description .= '<p>' . Mage::app()->getLocale()->currency(Mage::app()->getStore()->
getCurrentCurrencyCode())->getSymbol() . " " . $this->helper('tax')->getPrice($product, $product->getFinalPrice(), true) . '</p>';

$description .= '</td></tr></table>';
$rssObj = $args['rssObj'];
$data = array(
'title'         => $product->getName(),
'link'          => $product->getProductUrl(),
'description'   => $description,
);

$rssObj->_addEntry($data);
}
}

You are almost done, now to display your new RSS custom feeds on the RSS page on your Magento (http://<magento_root>/rss) you only need to add them to the DB, simply add them to the table “core_config_data”:

  • Scope: default
  • Scope_id: 0
  • path: rss/catalog/bestseller
  • value: 1

You are done! Hope you find this guide useful.



Leave a Comment