Magento Themes and Magento Extensions

Magento adding you own custom RSS Feeds

Share

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:

01.<?xml version="1.0"?>
02.<config>
03.<modules>
04.<Simplicissimus_Rss>
05.<version>0.1.0</version>
06.</Simplicissimus_Rss>
07.</modules>
08.<global>
09.<blocks>
10.<rss>
11.<rewrite>
12.<list>Simplicissimus_Rss_Block_List</list>
13.<catalog_category>Simplicissimus_Rss_Block_Catalog_Category</catalog_category>
14.<catalog_bestseller>Simplicissimus_Rss_Block_Catalog_Bestseller</catalog_bestseller>
15.<catalog_bestsellerweekly>Simplicissimus_Rss_Block_Catalog_Bestsellerweekly</catalog_bestsellerweekly>
16.<catalog_bestsellermonthly>Simplicissimus_Rss_Block_Catalog_Bestsellermonthly</catalog_bestsellermonthly>
17.<catalog_bestsellernarcissus>Simplicissimus_Rss_Block_Catalog_Bestsellernarcissus</catalog_bestsellernarcissus>
18.<catalog_worstseller>Simplicissimus_Rss_Block_Catalog_Worstseller</catalog_worstseller>
19.</rewrite>
20.</rss>
21.</blocks>
22.</global>
23.<frontend>
24.<routers>
25.<rss>
26.<use>standard</use>
27.<args>
28.<modules>
29.<Simplicissimus_Rssbefore="Mage_Rss">Simplicissimus_Rss</Simplicissimus_Rss>
30.</modules>
31.</args>
32.</rss>
33.</routers>
34.</frontend>
35.</config>

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

01.<?php
02.// Controllers are not autoloaded so we will have to do it manually:
03.require_once 'Mage/Rss/controllers/CatalogController.php';
04.class Simplicissimus_Rss_CatalogController extendsMage_Rss_CatalogController
05.{
06.public function bestsellerAction()
07.{
08.$this->checkFeedEnable('bestseller');
09.$this->loadLayout(false);
10.$this->renderLayout();
11.}
12.
13.public function bestsellerweeklyAction()
14.{
15.$this->checkFeedEnable('bestsellerweekly');
16.$this->loadLayout(false);
17.$this->renderLayout();
18.}
19.
20.public function bestsellermonthlyAction()
21.{
22.$this->checkFeedEnable('bestsellermonthly');
23.$this->loadLayout(false);
24.$this->renderLayout();
25.}
26.
27.public function bestsellernarcissusAction()
28.{
29.$this->checkFeedEnable('bestsellernarcissus');
30.$this->loadLayout(false);
31.$this->renderLayout();
32.}
33.
34.public function worstsellerAction()
35.{
36.$this->checkFeedEnable('worstseller');
37.$this->loadLayout(false);
38.$this->renderLayout();
39.}
40.}

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

01.<?php
02.class Simplicissimus_Rss_Block_List extends Mage_Rss_Block_List
03.{
04.
05.public function getRssMiscFeeds()
06.{
07.$this->resetRssFeed();
08.//$this->NewProductRssFeed();
09.//$this->SpecialProductRssFeed();
10.$this->SalesRuleProductRssFeed();
11.
12.// SBF
13.$this->BestsellerRssFeed();
14.$this->BestsellerWeeklyRssFeed();
15.$this->BestsellerMonthlyRssFeed();
16.$this->BestsellerNarcissusRssFeed();
17.$this->WorstsellerRssFeed();
18.
19.return $this->getRssFeeds();
20.}
21.
22.public function BestsellerRssFeed()
23.{
24.$path = self::XML_PATH_RSS_METHODS.'/catalog/bestseller';
25.if((bool)Mage::getStoreConfig($path)){
26.$this->addRssFeed($path, $this->__('Ebook Bestseller (All time)'));
27.}
28.}
29.
30.public function BestsellerWeeklyRssFeed()
31.{
32.$path = self::XML_PATH_RSS_METHODS.'/catalog/bestsellerweekly';
33.if((bool)Mage::getStoreConfig($path)){
34.$this->addRssFeed($path, $this->__('Ebook Bestseller (Ultima settimana)'));
35.}
36.}
37.
38.public function BestsellerMonthlyRssFeed()
39.{
40.$path = self::XML_PATH_RSS_METHODS.'/catalog/bestsellermonthly';
41.if((bool)Mage::getStoreConfig($path)){
42.$this->addRssFeed($path, $this->__('Ebook Bestseller (Ultimo mese)'));
43.}
44.}
45.
46.public function BestsellerNarcissusRssFeed()
47.{
48.$path = self::XML_PATH_RSS_METHODS.'/catalog/bestsellernarcissus';
49.if((bool)Mage::getStoreConfig($path)){
50.$this->addRssFeed($path, $this->__('Ebook Bestseller (Narcissus Self Published)'));
51.}
52.}
53.
54.public function WorstsellerRssFeed()
55.{
56.$path = self::XML_PATH_RSS_METHODS.'/catalog/worstseller';
57.if((bool)Mage::getStoreConfig($path)){
58.$this->addRssFeed($path, $this->__('Ebook meno venduti'));
59.}
60.}
61.
62.}

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:

01.<?php
02.class Simplicissimus_Rss_Block_Catalog_Bestseller extendsMage_Rss_Block_Abstract
03.{
04.
05./**
06.* Cache tag constant for feed reviews
07.*
08.* @var string
09.*/
10.const CACHE_TAG = 'block_html_rss_catalog_bestseller';
11.
12.protected function _construct()
13.{
14.$this->setCacheTags(array(self::CACHE_TAG));
15./*
16.* setting cache to save the rss for 10 minutes
17.*/
18.$this->setCacheKey('rss_catalog_bestseller');
19.$this->setCacheLifetime(600);
20.}
21.
22.protected function _toHtml()
23.{
24.$newurl = Mage::getUrl('rss/catalog/bestseller');
25.$title = Mage::helper('rss')->__('Ebook Bestseller (All time)');
26.
27.$rssObj = Mage::getModel('rss/rss');
28.$data = array('title' => $title,
29.'description' => $title,
30.'link' => $newurl,
31.'charset' => 'UTF-8',
32.);
33.$rssObj->_addHeader($data);
34.
35.$_productCollection= Mage::getResourceModel('reports/product_collection')
36.->addAttributeToSelect('*')
37.->addOrderedQty()
38.->addFieldToFilter('type_id', array('like'=>'downloadable'))
39.->addAttributeToFilter('status', 1)
40.->setOrder('ordered_qty', 'desc')
41.->setCurPage(1)
42.->setPageSize(20);
43.
44.if ($_productCollection) {
45.$args = array('rssObj' => $rssObj);
46.foreach ($_productCollection as $_product) {
47.$args['product'] = $_product;
48.$this->addBestsellerXmlCallback($args);
49.}
50.}
51.
52.return $rssObj->createRssXml();
53.}
54.
55.public function addBestsellerXmlCallback($args)
56.{
57.$product= Mage::getModel('catalog/product')->load($args['product']['entity_id']);
58.
59.Mage::dispatchEvent('rss_catalog_category_xml_callback', $args);
60.
61.if ( $product->getTypeId() == "downloadable" )
62.$extendedDescr = "<p><strong>Autore: </strong>". $product->getStealth_authors() . "<br /><strong>Editore: </strong>". $product->getStealth_publisher() . "<br /><strong>Protezione: </strong>". $product->getStealth_drm();
63.else
64.$extendedDescr = "<p>" . $product->getShortDescription() . "</p>";
65.
66.//Mage::log($product->getData());
67.
68.$description = '<table><tr>'
69.. '<td><a href="'.$product->getProductUrl().'"><img src="'
70.. $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75, 75)
71.. '" border="0" align="left" height="75" width="75"></a></td>'
72.. '<td  style="text-decoration:none;">' . $extendedDescr;
73.
74.$description .= '<p>'. Mage::app()->getLocale()->currency(Mage::app()->getStore()->
75.getCurrentCurrencyCode())->getSymbol() . " ". $this->helper('tax')->getPrice($product, $product->getFinalPrice(), true) . '</p>';
76.
77.$description .= '</td></tr></table>';
78.$rssObj = $args['rssObj'];
79.$data = array(
80.'title' => $product->getName(),
81.'link' => $product->getProductUrl(),
82.'description' => $description,
83.);
84.
85.$rssObj->_addEntry($data);
86.}
87.}

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

Comments are closed.