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:

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”:

You are done! Hope you find this guide useful.

Tags: , , , , , , ,

Post simili:

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

7 Responses to “Magento adding you own custom RSS Feeds”
  1. Magento adding you own custom RSS Feeds
    08:52 on December 15th, 2012

    […] sourceprint? 01.<?php 02.class Simplicissimus_Rss_Block_Catalog_Bestseller extendsMage_Rss_Block_Abstract […]

  2. Magento adding you own custom RSS Feeds
    03:44 on December 4th, 2012

    […] we need blocks, first of all create a file Block/List.php: view sourceprint? 01.<?php 02.class Simplicissimus_Rss_Block_List extends Mage_Rss_Block_List 03.{ 04. 05.public […]

  3. jaz
    19:57 on December 18th, 2011

    I couldn’t get it to work :-/

  4. Shawn Dahl
    14:03 on September 28th, 2011

    This product is really a collection of other products that enables us to display several similar products on a single page.

  5. alabama movers
    12:34 on September 27th, 2011

    I need to create an RSS feed of all products on the store, in case anyone knows how that’s possible, pls inform.

  6. Mary
    15:27 on June 17th, 2011

    Thx for the guide! I found it very useful :)

  7. Max Furniture
    08:59 on June 16th, 2011

    Nice article but I don’t understand whether you’re just showing how to add translations to a custom module,f you want to re-use a module on another site you would have to copy over the extra translations module.Also, I think codepool should be codePool.Nice article though, I read your site a lot.

Leave a comment

(required)

(required)


*

Categories