Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_CatalogRule_Helper_Data
  • Mage_CatalogRule_Model_Flag
  • Mage_CatalogRule_Model_Mysql4_Rule
  • Mage_CatalogRule_Model_Mysql4_Rule_Collection
  • Mage_CatalogRule_Model_Mysql4_Rule_Product_Price
  • Mage_CatalogRule_Model_Mysql4_Rule_Product_Price_Collection
  • Mage_CatalogRule_Model_Observer
  • Mage_CatalogRule_Model_Resource_Rule
  • Mage_CatalogRule_Model_Resource_Rule_Collection
  • Mage_CatalogRule_Model_Resource_Rule_Product_Price
  • Mage_CatalogRule_Model_Resource_Rule_Product_Price_Collection
  • Mage_CatalogRule_Model_Rule
  • Mage_CatalogRule_Model_Rule_Action_Collection
  • Mage_CatalogRule_Model_Rule_Action_Product
  • Mage_CatalogRule_Model_Rule_Condition_Combine
  • Mage_CatalogRule_Model_Rule_Condition_Product
  • Mage_CatalogRule_Model_Rule_Product_Price
  • Overview
  • Package
  • Class
  • Tree
  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_CatalogRule
 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 Price rules observer model
 29:  */
 30: class Mage_CatalogRule_Model_Observer
 31: {
 32:     /**
 33:      * Store calculated catalog rules prices for products
 34:      * Prices collected per website, customer group, date and product
 35:      *
 36:      * @var array
 37:      */
 38:     protected $_rulePrices = array();
 39: 
 40:     /**
 41:      * Apply all catalog price rules for specific product
 42:      *
 43:      * @param   Varien_Event_Observer $observer
 44:      * @return  Mage_CatalogRule_Model_Observer
 45:      */
 46:     public function applyAllRulesOnProduct($observer)
 47:     {
 48:         $product = $observer->getEvent()->getProduct();
 49:         if ($product->getIsMassupdate()) {
 50:             return;
 51:         }
 52: 
 53:         $productWebsiteIds = $product->getWebsiteIds();
 54: 
 55:         $rules = Mage::getModel('catalogrule/rule')->getCollection()
 56:             ->addFieldToFilter('is_active', 1);
 57: 
 58:         foreach ($rules as $rule) {
 59:             $websiteIds = array_intersect($productWebsiteIds, $rule->getWebsiteIds());
 60:             $rule->applyToProduct($product, $websiteIds);
 61:         }
 62: 
 63:         return $this;
 64:     }
 65: 
 66:     /**
 67:      * Apply all price rules for current date.
 68:      * Handle cataolg_product_import_after event
 69:      *
 70:      * @param   Varien_Event_Observer $observer
 71:      *
 72:      * @return  Mage_CatalogRule_Model_Observer
 73:      */
 74:     public function applyAllRules($observer)
 75:     {
 76:         $resource = Mage::getResourceSingleton('catalogrule/rule');
 77:         $resource->applyAllRulesForDateRange($resource->formatDate(mktime(0,0,0)));
 78:         Mage::getModel('catalogrule/flag')->loadSelf()
 79:             ->setState(0)
 80:             ->save();
 81: 
 82:         return $this;
 83:     }
 84: 
 85:     /**
 86:      * Apply catalog price rules to product on frontend
 87:      *
 88:      * @param   Varien_Event_Observer $observer
 89:      *
 90:      * @return  Mage_CatalogRule_Model_Observer
 91:      */
 92:     public function processFrontFinalPrice($observer)
 93:     {
 94:         $product    = $observer->getEvent()->getProduct();
 95:         $pId        = $product->getId();
 96:         $storeId    = $product->getStoreId();
 97: 
 98:         if ($observer->hasDate()) {
 99:             $date = $observer->getEvent()->getDate();
100:         } else {
101:             $date = Mage::app()->getLocale()->storeTimeStamp($storeId);
102:         }
103: 
104:         if ($observer->hasWebsiteId()) {
105:             $wId = $observer->getEvent()->getWebsiteId();
106:         } else {
107:             $wId = Mage::app()->getStore($storeId)->getWebsiteId();
108:         }
109: 
110:         if ($observer->hasCustomerGroupId()) {
111:             $gId = $observer->getEvent()->getCustomerGroupId();
112:         } elseif ($product->hasCustomerGroupId()) {
113:             $gId = $product->getCustomerGroupId();
114:         } else {
115:             $gId = Mage::getSingleton('customer/session')->getCustomerGroupId();
116:         }
117: 
118:         $key = "$date|$wId|$gId|$pId";
119:         if (!isset($this->_rulePrices[$key])) {
120:             $rulePrice = Mage::getResourceModel('catalogrule/rule')
121:                 ->getRulePrice($date, $wId, $gId, $pId);
122:             $this->_rulePrices[$key] = $rulePrice;
123:         }
124:         if ($this->_rulePrices[$key]!==false) {
125:             $finalPrice = min($product->getData('final_price'), $this->_rulePrices[$key]);
126:             $product->setFinalPrice($finalPrice);
127:         }
128:         return $this;
129:     }
130: 
131:     /**
132:      * Apply catalog price rules to product in admin
133:      *
134:      * @param   Varien_Event_Observer $observer
135:      *
136:      * @return  Mage_CatalogRule_Model_Observer
137:      */
138:     public function processAdminFinalPrice($observer)
139:     {
140:         $product = $observer->getEvent()->getProduct();
141:         $storeId = $product->getStoreId();
142:         $date = Mage::app()->getLocale()->storeDate($storeId);
143:         $key = false;
144: 
145:         if ($ruleData = Mage::registry('rule_data')) {
146:             $wId = $ruleData->getWebsiteId();
147:             $gId = $ruleData->getCustomerGroupId();
148:             $pId = $product->getId();
149: 
150:             $key = "$date|$wId|$gId|$pId";
151:         }
152:         elseif (!is_null($product->getWebsiteId()) && !is_null($product->getCustomerGroupId())) {
153:             $wId = $product->getWebsiteId();
154:             $gId = $product->getCustomerGroupId();
155:             $pId = $product->getId();
156:             $key = "$date|$wId|$gId|$pId";
157:         }
158: 
159:         if ($key) {
160:             if (!isset($this->_rulePrices[$key])) {
161:                 $rulePrice = Mage::getResourceModel('catalogrule/rule')
162:                     ->getRulePrice($date, $wId, $gId, $pId);
163:                 $this->_rulePrices[$key] = $rulePrice;
164:             }
165:             if ($this->_rulePrices[$key]!==false) {
166:                 $finalPrice = min($product->getData('final_price'), $this->_rulePrices[$key]);
167:                 $product->setFinalPrice($finalPrice);
168:             }
169:         }
170: 
171:         return $this;
172:     }
173: 
174:     /**
175:      * Calculate price using catalog price rules of configurable product
176:      *
177:      * @param Varien_Event_Observer $observer
178:      *
179:      * @return Mage_CatalogRule_Model_Observer
180:      */
181:     public function catalogProductTypeConfigurablePrice(Varien_Event_Observer $observer)
182:     {
183:         $product = $observer->getEvent()->getProduct();
184:         if ($product instanceof Mage_Catalog_Model_Product
185:             && $product->getConfigurablePrice() !== null
186:         ) {
187:             $configurablePrice = $product->getConfigurablePrice();
188:             $productPriceRule = Mage::getModel('catalogrule/rule')->calcProductPriceRule($product, $configurablePrice);
189:             if ($productPriceRule !== null) {
190:                 $product->setConfigurablePrice($productPriceRule);
191:             }
192:         }
193: 
194:         return $this;
195:     }
196: 
197:     /**
198:      * Daily update catalog price rule by cron
199:      * Update include interval 3 days - current day - 1 days before + 1 days after
200:      * This method is called from cron process, cron is working in UTC time and
201:      * we should generate data for interval -1 day ... +1 day
202:      *
203:      * @param   Varien_Event_Observer $observer
204:      *
205:      * @return  Mage_CatalogRule_Model_Observer
206:      */
207:     public function dailyCatalogUpdate($observer)
208:     {
209:         Mage::getResourceSingleton('catalogrule/rule')->applyAllRulesForDateRange();
210: 
211:         return $this;
212:     }
213: 
214:     /**
215:      * Clean out calculated catalog rule prices for products
216:      */
217:     public function flushPriceCache()
218:     {
219:         $this->_rulePrices = array();
220:     }
221: 
222:     /**
223:      * Calculate minimal final price with catalog rule price
224:      *
225:      * @param Varien_Event_Observer $observer
226:      * @return Mage_CatalogRule_Model_Observer
227:      */
228:     public function prepareCatalogProductPriceIndexTable(Varien_Event_Observer $observer)
229:     {
230:         $select             = $observer->getEvent()->getSelect();
231: 
232:         $indexTable         = $observer->getEvent()->getIndexTable();
233:         $entityId           = $observer->getEvent()->getEntityId();
234:         $customerGroupId    = $observer->getEvent()->getCustomerGroupId();
235:         $websiteId          = $observer->getEvent()->getWebsiteId();
236:         $websiteDate        = $observer->getEvent()->getWebsiteDate();
237:         $updateFields       = $observer->getEvent()->getUpdateFields();
238: 
239:         Mage::getSingleton('catalogrule/rule_product_price')
240:             ->applyPriceRuleToIndexTable($select, $indexTable, $entityId, $customerGroupId, $websiteId,
241:                 $updateFields, $websiteDate);
242: 
243:         return $this;
244:     }
245: 
246:     /**
247:      * Check rules that contains affected attribute
248:      * If rules were found they will be set to inactive and notice will be add to admin session
249:      *
250:      * @param string $attributeCode
251:      *
252:      * @return Mage_CatalogRule_Model_Observer
253:      */
254:     protected function _checkCatalogRulesAvailability($attributeCode)
255:     {
256:         /* @var $collection Mage_CatalogRule_Model_Mysql4_Rule_Collection */
257:         $collection = Mage::getResourceModel('catalogrule/rule_collection')
258:             ->addAttributeInConditionFilter($attributeCode);
259: 
260:         $disabledRulesCount = 0;
261:         foreach ($collection as $rule) {
262:             /* @var $rule Mage_CatalogRule_Model_Rule */
263:             $rule->setIsActive(0);
264:             /* @var $rule->getConditions() Mage_CatalogRule_Model_Rule_Condition_Combine */
265:             $this->_removeAttributeFromConditions($rule->getConditions(), $attributeCode);
266:             $rule->save();
267: 
268:             $disabledRulesCount++;
269:         }
270: 
271:         if ($disabledRulesCount) {
272:             Mage::getModel('catalogrule/rule')->applyAll();
273:             Mage::getSingleton('adminhtml/session')->addWarning(
274:                 Mage::helper('catalogrule')->__('%d Catalog Price Rules based on "%s" attribute have been disabled.', $disabledRulesCount, $attributeCode));
275:         }
276: 
277:         return $this;
278:     }
279: 
280:     /**
281:      * Remove catalog attribute condition by attribute code from rule conditions
282:      *
283:      * @param Mage_CatalogRule_Model_Rule_Condition_Combine $combine
284:      *
285:      * @param string $attributeCode
286:      */
287:     protected function _removeAttributeFromConditions($combine, $attributeCode)
288:     {
289:         $conditions = $combine->getConditions();
290:         foreach ($conditions as $conditionId => $condition) {
291:             if ($condition instanceof Mage_CatalogRule_Model_Rule_Condition_Combine) {
292:                 $this->_removeAttributeFromConditions($condition, $attributeCode);
293:             }
294:             if ($condition instanceof Mage_Rule_Model_Condition_Product_Abstract) {
295:                 if ($condition->getAttribute() == $attributeCode) {
296:                     unset($conditions[$conditionId]);
297:                 }
298:             }
299:         }
300:         $combine->setConditions($conditions);
301:     }
302: 
303:     /**
304:      * After save attribute if it is not used for promo rules already check rules for containing this attribute
305:      *
306:      * @param Varien_Event_Observer $observer
307:      *
308:      * @return Mage_CatalogRule_Model_Observer
309:      */
310:     public function catalogAttributeSaveAfter(Varien_Event_Observer $observer)
311:     {
312:         $attribute = $observer->getEvent()->getAttribute();
313:         if ($attribute->dataHasChangedFor('is_used_for_promo_rules') && !$attribute->getIsUsedForPromoRules()) {
314:             $this->_checkCatalogRulesAvailability($attribute->getAttributeCode());
315:         }
316: 
317:         return $this;
318:     }
319: 
320:     /**
321:      * After delete attribute check rules that contains deleted attribute
322:      *
323:      * @param Varien_Event_Observer $observer
324:      * @return Mage_CatalogRule_Model_Observer
325:      */
326:     public function catalogAttributeDeleteAfter(Varien_Event_Observer $observer)
327:     {
328:         $attribute = $observer->getEvent()->getAttribute();
329:         if ($attribute->getIsUsedForPromoRules()) {
330:             $this->_checkCatalogRulesAvailability($attribute->getAttributeCode());
331:         }
332: 
333:         return $this;
334:     }
335: 
336:     public function prepareCatalogProductCollectionPrices(Varien_Event_Observer $observer)
337:     {
338:         /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
339:         $collection = $observer->getEvent()->getCollection();
340:         $store      = Mage::app()->getStore($observer->getEvent()->getStoreId());
341:         $websiteId  = $store->getWebsiteId();
342:         if ($observer->getEvent()->hasCustomerGroupId()) {
343:             $groupId = $observer->getEvent()->getCustomerGroupId();
344:         } else {
345:             /* @var $session Mage_Customer_Model_Session */
346:             $session = Mage::getSingleton('customer/session');
347:             if ($session->isLoggedIn()) {
348:                 $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
349:             } else {
350:                 $groupId = Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
351:             }
352:         }
353:         if ($observer->getEvent()->hasDate()) {
354:             $date = $observer->getEvent()->getDate();
355:         } else {
356:             $date = Mage::app()->getLocale()->storeTimeStamp($store);
357:         }
358: 
359:         $productIds = array();
360:         /* @var $product Mage_Core_Model_Product */
361:         foreach ($collection as $product) {
362:             $key = implode('|', array($date, $websiteId, $groupId, $product->getId()));
363:             if (!isset($this->_rulePrices[$key])) {
364:                 $productIds[] = $product->getId();
365:             }
366:         }
367: 
368:         if ($productIds) {
369:             $rulePrices = Mage::getResourceModel('catalogrule/rule')
370:                 ->getRulePrices($date, $websiteId, $groupId, $productIds);
371:             foreach ($productIds as $productId) {
372:                 $key = implode('|', array($date, $websiteId, $groupId, $productId));
373:                 $this->_rulePrices[$key] = isset($rulePrices[$productId]) ? $rulePrices[$productId] : false;
374:             }
375:         }
376: 
377:         return $this;
378:     }
379: 
380:     /**
381:      * Create catalog rule relations for imported products
382:      *
383:      * @param Varien_Event_Observer $observer
384:      */
385:     public function createCatalogRulesRelations(Varien_Event_Observer $observer)
386:     {
387:         $adapter = $observer->getEvent()->getAdapter();
388:         $affectedEntityIds = $adapter->getAffectedEntityIds();
389: 
390:         if (empty($affectedEntityIds)) {
391:             return;
392:         }
393: 
394:         $rules = Mage::getModel('catalogrule/rule')->getCollection()
395:             ->addFieldToFilter('is_active', 1);
396: 
397:         foreach ($rules as $rule) {
398:             $rule->setProductsFilter($affectedEntityIds);
399:             Mage::getResourceSingleton('catalogrule/rule')->updateRuleProductData($rule);
400:         }
401:     }
402: }
403: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0