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_Bundle
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: /**
29: * Bundle Product Price Index
30: *
31: * @method Mage_Bundle_Model_Resource_Price_Index _getResource()
32: * @method Mage_Bundle_Model_Resource_Price_Index getResource()
33: * @method Mage_Bundle_Model_Price_Index setEntityId(int $value)
34: * @method int getWebsiteId()
35: * @method Mage_Bundle_Model_Price_Index setWebsiteId(int $value)
36: * @method int getCustomerGroupId()
37: * @method Mage_Bundle_Model_Price_Index setCustomerGroupId(int $value)
38: * @method float getMinPrice()
39: * @method Mage_Bundle_Model_Price_Index setMinPrice(float $value)
40: * @method float getMaxPrice()
41: * @method Mage_Bundle_Model_Price_Index setMaxPrice(float $value)
42: *
43: * @category Mage
44: * @package Mage_Bundle
45: * @author Magento Core Team <core@magentocommerce.com>
46: */
47: class Mage_Bundle_Model_Price_Index extends Mage_Core_Model_Abstract
48: {
49: /**
50: * Initialize resource model
51: *
52: */
53: protected function _construct()
54: {
55: $this->_init('bundle/price_index');
56: }
57:
58: /**
59: * Retrieve resource instance wrapper
60: *
61: * @return Mage_Bundle_Model_Mysql4_Price_Index
62: */
63: protected function _getResource()
64: {
65: return parent::_getResource();
66: }
67:
68: /**
69: * Reindex Product price
70: *
71: * @param int $productId
72: * @param int $priceType
73: * @return Mage_Bundle_Model_Price_Index
74: */
75: protected function _reindexProduct($productId, $priceType)
76: {
77: $this->_getResource()->reindexProduct($productId, $priceType);
78: return $this;
79: }
80:
81: /**
82: * Reindex Bundle product Price Index
83: *
84: * @param Mage_Core_Model_Product|Mage_Catalog_Model_Product_Condition_Interface|array|int $products
85: * @return Mage_Bundle_Model_Price_Index
86: */
87: public function reindex($products = null)
88: {
89: $this->_getResource()->reindex($products);
90: return $this;
91: }
92:
93: /**
94: * Add bundle price range index to Product collection
95: *
96: * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
97: * @return Mage_Bundle_Model_Price_Index
98: */
99: public function addPriceIndexToCollection($collection)
100: {
101: $productObjects = array();
102: $productIds = array();
103: foreach ($collection->getItems() as $product) {
104: /* @var $product Mage_Catalog_Model_Product */
105: if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
106: $productIds[] = $product->getEntityId();
107: $productObjects[$product->getEntityId()] = $product;
108: }
109: }
110: $websiteId = Mage::app()->getStore($collection->getStoreId())
111: ->getWebsiteId();
112: $groupId = Mage::getSingleton('customer/session')
113: ->getCustomerGroupId();
114:
115: $addOptionsToResult = false;
116: $prices = $this->_getResource()->loadPriceIndex($productIds, $websiteId, $groupId);
117: foreach ($productIds as $productId) {
118: if (isset($prices[$productId])) {
119: $productObjects[$productId]
120: ->setData('_price_index', true)
121: ->setData('_price_index_min_price', $prices[$productId]['min_price'])
122: ->setData('_price_index_max_price', $prices[$productId]['max_price']);
123: }
124: else {
125: $addOptionsToResult = true;
126: }
127: }
128:
129: if ($addOptionsToResult) {
130: $collection->addOptionsToResult();
131: }
132:
133: return $this;
134: }
135:
136: /**
137: * Add price index to bundle product after load
138: *
139: * @param Mage_Catalog_Model_Product $product
140: * @return Mage_Bundle_Model_Price_Index
141: */
142: public function addPriceIndexToProduct($product)
143: {
144: $websiteId = $product->getStore()->getWebsiteId();
145: $groupId = Mage::getSingleton('customer/session')
146: ->getCustomerGroupId();
147: $prices = $this->_getResource()
148: ->loadPriceIndex($product->getId(), $websiteId, $groupId);
149: if (isset($prices[$product->getId()])) {
150: $product->setData('_price_index', true)
151: ->setData('_price_index_min_price', $prices[$product->getId()]['min_price'])
152: ->setData('_price_index_max_price', $prices[$product->getId()]['max_price']);
153: }
154: return $this;
155: }
156: }
157: