1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Catalog product related items block
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34:
35: class Mage_Catalog_Block_Product_List_Upsell extends Mage_Catalog_Block_Product_Abstract
36: {
37: /**
38: * Default MAP renderer type
39: *
40: * @var string
41: */
42: protected $_mapRenderer = 'msrp_noform';
43:
44: protected $_columnCount = 4;
45:
46: protected $_items;
47:
48: protected $_itemCollection;
49:
50: protected $_itemLimits = array();
51:
52: protected function _prepareData()
53: {
54: $product = Mage::registry('product');
55: /* @var $product Mage_Catalog_Model_Product */
56: $this->_itemCollection = $product->getUpSellProductCollection()
57: ->setPositionOrder()
58: ->addStoreFilter()
59: ;
60: if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
61: Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
62: Mage::getSingleton('checkout/session')->getQuoteId()
63: );
64:
65: $this->_addProductAttributesAndPrices($this->_itemCollection);
66: }
67: // Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
68: Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);
69:
70: if ($this->getItemLimit('upsell') > 0) {
71: $this->_itemCollection->setPageSize($this->getItemLimit('upsell'));
72: }
73:
74: $this->_itemCollection->load();
75:
76: /**
77: * Updating collection with desired items
78: */
79: Mage::dispatchEvent('catalog_product_upsell', array(
80: 'product' => $product,
81: 'collection' => $this->_itemCollection,
82: 'limit' => $this->getItemLimit()
83: ));
84:
85: foreach ($this->_itemCollection as $product) {
86: $product->setDoNotUseCategoryId(true);
87: }
88:
89: return $this;
90: }
91:
92: protected function _beforeToHtml()
93: {
94: $this->_prepareData();
95: return parent::_beforeToHtml();
96: }
97:
98: public function getItemCollection()
99: {
100: return $this->_itemCollection;
101: }
102:
103: public function getItems()
104: {
105: if (is_null($this->_items)) {
106: $this->_items = $this->getItemCollection()->getItems();
107: }
108: return $this->_items;
109: }
110:
111: public function getRowCount()
112: {
113: return ceil(count($this->getItemCollection()->getItems())/$this->getColumnCount());
114: }
115:
116: public function setColumnCount($columns)
117: {
118: if (intval($columns) > 0) {
119: $this->_columnCount = intval($columns);
120: }
121: return $this;
122: }
123:
124: public function getColumnCount()
125: {
126: return $this->_columnCount;
127: }
128:
129: public function resetItemsIterator()
130: {
131: $this->getItems();
132: reset($this->_items);
133: }
134:
135: public function getIterableItem()
136: {
137: $item = current($this->_items);
138: next($this->_items);
139: return $item;
140: }
141:
142: /**
143: * Set how many items we need to show in upsell block
144: * Notice: this parametr will be also applied
145: *
146: * @param string $type
147: * @param int $limit
148: * @return Mage_Catalog_Block_Product_List_Upsell
149: */
150: public function setItemLimit($type, $limit)
151: {
152: if (intval($limit) > 0) {
153: $this->_itemLimits[$type] = intval($limit);
154: }
155: return $this;
156: }
157:
158: public function getItemLimit($type = '')
159: {
160: if ($type == '') {
161: return $this->_itemLimits;
162: }
163: if (isset($this->_itemLimits[$type])) {
164: return $this->_itemLimits[$type];
165: }
166: else {
167: return 0;
168: }
169: }
170: }
171: