1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class extends Mage_Rss_Block_Abstract
35: {
36:
37: 38: 39: 40: 41:
42: const CACHE_TAG = 'block_html_rss_catalog_notifystock';
43:
44: 45: 46: 47: 48:
49: protected function _construct()
50: {
51: $this->setCacheTags(array(self::CACHE_TAG));
52: 53: 54:
55: $this->setCacheKey('rss_catalog_notifystock');
56: $this->setCacheLifetime(600);
57: }
58:
59: 60: 61: 62: 63:
64: protected function _toHtml()
65: {
66: $newUrl = Mage::getUrl('rss/catalog/notifystock');
67: $title = Mage::helper('rss')->__('Low Stock Products');
68:
69: $rssObj = Mage::getModel('rss/rss');
70: $data = array(
71: 'title' => $title,
72: 'description' => $title,
73: 'link' => $newUrl,
74: 'charset' => 'UTF-8',
75: );
76: $rssObj->_addHeader($data);
77:
78: $configManageStock = (int) Mage::getStoreConfigFlag(
79: Mage_CatalogInventory_Model_Stock_Item::XML_PATH_MANAGE_STOCK);
80: $globalNotifyStockQty = (float) Mage::getStoreConfig(
81: Mage_CatalogInventory_Model_Stock_Item::XML_PATH_NOTIFY_STOCK_QTY);
82: Mage::helper('rss')->disableFlat();
83:
84: $product = Mage::getModel('catalog/product');
85:
86: $collection = $product->getCollection();
87: $stockItemTable = $collection->getTable('cataloginventory/stock_item');
88:
89: $stockItemWhere = '({{table}}.low_stock_date is not null) '
90: . " AND ( ({{table}}.use_config_manage_stock=1 AND {$configManageStock}=1)"
91: . " AND {{table}}.qty < "
92: . "IF({$stockItemTable}.`use_config_notify_stock_qty`, {$globalNotifyStockQty}, {{table}}.notify_stock_qty)"
93: . ' OR ({{table}}.use_config_manage_stock=0 AND {{table}}.manage_stock=1) )';
94:
95: $collection
96: ->addAttributeToSelect('name', true)
97: ->joinTable('cataloginventory/stock_item', 'product_id=entity_id',
98: array(
99: 'qty'=>'qty',
100: 'notify_stock_qty'=>'notify_stock_qty',
101: 'use_config' => 'use_config_notify_stock_qty',
102: 'low_stock_date' => 'low_stock_date'),
103: $stockItemWhere, 'inner')
104: ->setOrder('low_stock_date');
105:
106: $collection->addAttributeToFilter('status',
107: array('in' => Mage::getSingleton('catalog/product_status')->getVisibleStatusIds()));
108: Mage::dispatchEvent('rss_catalog_notify_stock_collection_select', array('collection' => $collection));
109:
110: 111: 112: 113:
114: Mage::getSingleton('core/resource_iterator')->walk(
115: $collection->getSelect(),
116: array(array($this, 'addNotifyItemXmlCallback')),
117: array('rssObj'=> $rssObj, 'product'=>$product, 'globalQty' => $globalNotifyStockQty)
118: );
119:
120: return $rssObj->createRssXml();
121: }
122:
123: 124: 125: 126: 127: 128:
129: public function addNotifyItemXmlCallback($args)
130: {
131: $product = $args['product'];
132: $product->setData($args['row']);
133: $url = Mage::helper('adminhtml')->getUrl('adminhtml/catalog_product/edit/',
134: array('id' => $product->getId(), '_secure' => true, '_nosecret' => true));
135: $qty = 1 * $product->getQty();
136: $description = Mage::helper('rss')->__('%s has reached a quantity of %s.', $product->getName(), $qty);
137: $rssObj = $args['rssObj'];
138: $data = array(
139: 'title' => $product->getName(),
140: 'link' => $url,
141: 'description' => $description,
142: );
143: $rssObj->_addEntry($data);
144: }
145: }
146: