*/ class Mage_Rss_Block_Catalog_Special extends Mage_Rss_Block_Catalog_Abstract { /** * Zend_Date object for date comparsions * * @var Zend_Date $_currentDate */ protected static $_currentDate = null; protected function _construct() { /* * setting cache to save the rss for 10 minutes */ $this->setCacheKey('rss_catalog_special_'.$this->_getStoreId().'_'.$this->_getCustomerGroupId()); $this->setCacheLifetime(600); } protected function _toHtml() { //store id is store view id $storeId = $this->_getStoreId(); $websiteId = Mage::app()->getStore($storeId)->getWebsiteId(); //customer group id $customerGroupId = $this->_getCustomerGroupId(); $product = Mage::getModel('catalog/product'); $fields = array( 'final_price', 'price' ); $specials = $product->setStoreId($storeId)->getResourceCollection() ->addPriceDataFieldFilter('%s < %s', $fields) ->addPriceData($customerGroupId, $websiteId) ->addAttributeToSelect( array( 'name', 'short_description', 'description', 'price', 'thumbnail', 'special_price', 'special_to_date', 'msrp_enabled', 'msrp_display_actual_price_type', 'msrp' ), 'left' ) ->addAttributeToSort('name', 'asc') ; $newurl = Mage::getUrl('rss/catalog/special/store_id/' . $storeId); $title = Mage::helper('rss')->__('%s - Special Products', Mage::app()->getStore()->getFrontendName()); $lang = Mage::getStoreConfig('general/locale/code'); $rssObj = Mage::getModel('rss/rss'); $data = array('title' => $title, 'description' => $title, 'link' => $newurl, 'charset' => 'UTF-8', 'language' => $lang ); $rssObj->_addHeader($data); $results = array(); /* 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( $specials->getSelect(), array(array($this, 'addSpecialXmlCallback')), array('rssObj'=> $rssObj, 'results'=> &$results) ); if (sizeof($results)>0) { foreach($results as $result){ // render a row for RSS feed $product->setData($result); $html = sprintf('
%s', $product->getProductUrl(), $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75, 75), $this->helper('catalog/output')->productAttribute( $product, $product->getDescription(), 'description' ) ); // add price data if needed if ($product->getAllowedPriceInRss()) { if (Mage::helper('catalog')->canApplyMsrp($product)) { $html .= '
' . $this->__('Click for price') . ''; } else { $special = ''; if ($result['use_special']) { $special = '
' . Mage::helper('catalog')->__('Special Expires On: %s', $this->formatDate($result['special_to_date'], Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM)); } $html .= sprintf('

%s %s%s

', Mage::helper('catalog')->__('Price: %s', Mage::helper('core')->currency($result['price'])), Mage::helper('catalog')->__('Special Price: %s', Mage::helper('core')->currency($result['final_price'])), $special ); } } $html .= '
'; $rssObj->_addEntry(array( 'title' => $product->getName(), 'link' => $product->getProductUrl(), 'description' => $html )); } } return $rssObj->createRssXml(); } /** * Preparing data and adding to rss object * * @param array $args */ public function addSpecialXmlCallback($args) { if (!isset(self::$_currentDate)) { self::$_currentDate = new Zend_Date(); } // dispatch event to determine whether the product will eventually get to the result $product = new Varien_Object(array('allowed_in_rss' => true, 'allowed_price_in_rss' => true)); $args['product'] = $product; Mage::dispatchEvent('rss_catalog_special_xml_callback', $args); if (!$product->getAllowedInRss()) { return; } // add row to result and determine whether special price is active (less or equal to the final price) $row = $args['row']; $row['use_special'] = false; $row['allowed_price_in_rss'] = $product->getAllowedPriceInRss(); if (isset($row['special_to_date']) && $row['final_price'] <= $row['special_price'] && $row['allowed_price_in_rss'] ) { $compareDate = self::$_currentDate->compareDate($row['special_to_date'], Varien_Date::DATE_INTERNAL_FORMAT); if (-1 === $compareDate || 0 === $compareDate) { $row['use_special'] = true; } } $args['results'][] = $row; } /** * Function for comparing two items in collection * * @param Varien_Object $item1 * @param Varien_Object $item2 * @return boolean */ public function sortByStartDate($a, $b) { return $a['start_date']>$b['start_date'] ? -1 : ($a['start_date']<$b['start_date'] ? 1 : 0); } }