*/ class Mage_Rss_Block_Catalog_NotifyStock extends Mage_Rss_Block_Abstract { /** * Cache tag constant for feed notify stock * * @var string */ const CACHE_TAG = 'block_html_rss_catalog_notifystock'; /** * Constructor * * @return null */ protected function _construct() { $this->setCacheTags(array(self::CACHE_TAG)); /* * setting cache to save the rss for 10 minutes */ $this->setCacheKey('rss_catalog_notifystock'); $this->setCacheLifetime(600); } /** * Render RSS * * @return string */ protected function _toHtml() { $newUrl = Mage::getUrl('rss/catalog/notifystock'); $title = Mage::helper('rss')->__('Low Stock Products'); $rssObj = Mage::getModel('rss/rss'); $data = array( 'title' => $title, 'description' => $title, 'link' => $newUrl, 'charset' => 'UTF-8', ); $rssObj->_addHeader($data); $globalNotifyStockQty = (float) Mage::getStoreConfig( Mage_CatalogInventory_Model_Stock_Item::XML_PATH_NOTIFY_STOCK_QTY); Mage::helper('rss')->disableFlat(); /* @var $product Mage_Catalog_Model_Product */ $product = Mage::getModel('catalog/product'); /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */ $collection = $product->getCollection(); Mage::getResourceModel('cataloginventory/stock')->addLowStockFilter($collection, array( 'qty', 'notify_stock_qty', 'low_stock_date', 'use_config' => 'use_config_notify_stock_qty' )); $collection ->addAttributeToSelect('name', true) ->addAttributeToFilter('status', array('in' => Mage::getSingleton('catalog/product_status')->getVisibleStatusIds()) ) ->setOrder('low_stock_date'); Mage::dispatchEvent('rss_catalog_notify_stock_collection_select', array('collection' => $collection)); /* using resource iterator to load the data one by one instead of loading all at the same time. loading all data at the same time can cause the big memory allocation. */ Mage::getSingleton('core/resource_iterator')->walk( $collection->getSelect(), array(array($this, 'addNotifyItemXmlCallback')), array('rssObj'=> $rssObj, 'product'=>$product, 'globalQty' => $globalNotifyStockQty) ); return $rssObj->createRssXml(); } /** * Adds single product to feed * * @param array $args * @return void */ public function addNotifyItemXmlCallback($args) { $product = $args['product']; $product->setData($args['row']); $url = Mage::helper('adminhtml')->getUrl('adminhtml/catalog_product/edit/', array('id' => $product->getId(), '_secure' => true, '_nosecret' => true)); $qty = 1 * $product->getQty(); $description = Mage::helper('rss')->__('%s has reached a quantity of %s.', $product->getName(), $qty); $rssObj = $args['rssObj']; $data = array( 'title' => $product->getName(), 'link' => $url, 'description' => $description, ); $rssObj->_addEntry($data); } }