00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 class Mage_CatalogRule_Model_Observer
00031 {
00032 protected $_rulePrices = array();
00033
00034
00035
00036
00037
00038
00039
00040 public function applyAllRulesOnProduct($observer)
00041 {
00042 $product = $observer->getEvent()->getProduct();
00043 if ($product->getIsMassupdate()) {
00044 return;
00045 }
00046
00047 $productWebsiteIds = $product->getWebsiteIds();
00048
00049 $rules = Mage::getModel('catalogrule/rule')->getCollection()
00050 ->addFieldToFilter('is_active', 1);
00051
00052 foreach ($rules as $rule) {
00053 if (!is_array($rule->getWebsiteIds())) {
00054 $ruleWebsiteIds = (array)explode(',', $rule->getWebsiteIds());
00055 } else {
00056 $ruleWebsiteIds = $rule->getWebsiteIds();
00057 }
00058 $websiteIds = array_intersect($productWebsiteIds, $ruleWebsiteIds);
00059 $rule->applyToProduct($product, $websiteIds);
00060 }
00061 return $this;
00062 }
00063
00064
00065
00066
00067
00068
00069
00070 public function applyAllRules($observer)
00071 {
00072 $resource = Mage::getResourceSingleton('catalogrule/rule');
00073 $resource->applyAllRulesForDateRange($resource->formatDate(mktime(0,0,0)));
00074 Mage::app()->removeCache('catalog_rules_dirty');
00075 return $this;
00076 }
00077
00078
00079
00080
00081
00082
00083 public function processFrontFinalPrice($observer)
00084 {
00085 $product = $observer->getEvent()->getProduct();
00086 $pId = $product->getId();
00087 $storeId = $product->getStoreId();
00088
00089 if ($observer->hasDate()) {
00090 $date = $observer->getDate();
00091 } else {
00092 $date = Mage::app()->getLocale()->storeTimeStamp($storeId);
00093 }
00094
00095 if ($observer->hasWebsiteId()) {
00096 $wId = $observer->getWebsiteId();
00097 } else {
00098 $wId = Mage::app()->getStore($storeId)->getWebsiteId();
00099 }
00100
00101 if ($observer->hasCustomerGroupId()) {
00102 $gId = $observer->getCustomerGroupId();
00103 } else {
00104 $gId = Mage::getSingleton('customer/session')->getCustomerGroupId();
00105 }
00106
00107 $key = "$date|$wId|$gId|$pId";
00108 if (!isset($this->_rulePrices[$key])) {
00109 $rulePrice = Mage::getResourceModel('catalogrule/rule')
00110 ->getRulePrice($date, $wId, $gId, $pId);
00111 $this->_rulePrices[$key] = $rulePrice;
00112 }
00113 if ($this->_rulePrices[$key]!==false) {
00114 $finalPrice = min($product->getData('final_price'), $this->_rulePrices[$key]);
00115 $product->setFinalPrice($finalPrice);
00116 }
00117 return $this;
00118 }
00119
00120
00121
00122
00123
00124
00125 public function processAdminFinalPrice($observer)
00126 {
00127 $product = $observer->getEvent()->getProduct();
00128 $storeId = $product->getStoreId();
00129 $date = Mage::app()->getLocale()->storeDate($storeId);
00130 $key = false;
00131
00132 if ($ruleData = Mage::registry('rule_data')) {
00133 $wId = $ruleData->getWebsiteId();
00134 $gId = $ruleData->getCustomerGroupId();
00135 $pId = $product->getId();
00136
00137 $key = "$date|$wId|$gId|$pId";
00138 }
00139 elseif ($product->getWebsiteId() != null && $product->getCustomerGroupId() != null) {
00140 $wId = $product->getWebsiteId();
00141 $gId = $product->getCustomerGroupId();
00142 $pId = $product->getId();
00143 $key = "$date|$wId|$gId|$pId";
00144 }
00145
00146 if ($key) {
00147 if (!isset($this->_rulePrices[$key])) {
00148 $rulePrice = Mage::getResourceModel('catalogrule/rule')
00149 ->getRulePrice($date, $wId, $gId, $pId);
00150 $this->_rulePrices[$key] = $rulePrice;
00151 }
00152 if ($this->_rulePrices[$key]!==false) {
00153 $finalPrice = min($product->getData('final_price'), $this->_rulePrices[$key]);
00154 $product->setFinalPrice($finalPrice);
00155 }
00156 }
00157 return $this;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 public function dailyCatalogUpdate($observer)
00170 {
00171 Mage::getResourceSingleton('catalogrule/rule')->applyAllRulesForDateRange();
00172 return $this;
00173 }
00174
00175 public function flushPriceCache()
00176 {
00177 $this->_rulePrices = array();
00178 }
00179 }