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_SalesRule_Exception
  • Mage_SalesRule_Helper_Coupon
  • Mage_SalesRule_Helper_Data
  • Mage_SalesRule_Model_Coupon
  • Mage_SalesRule_Model_Coupon_Codegenerator
  • Mage_SalesRule_Model_Coupon_Massgenerator
  • Mage_SalesRule_Model_Mysql4_Coupon
  • Mage_SalesRule_Model_Mysql4_Coupon_Collection
  • Mage_SalesRule_Model_Mysql4_Coupon_Usage
  • Mage_SalesRule_Model_Mysql4_Report_Collection
  • Mage_SalesRule_Model_Mysql4_Report_Rule
  • Mage_SalesRule_Model_Mysql4_Report_Updatedat_Collection
  • Mage_SalesRule_Model_Mysql4_Rule
  • Mage_SalesRule_Model_Mysql4_Rule_Collection
  • Mage_SalesRule_Model_Mysql4_Rule_Customer
  • Mage_SalesRule_Model_Mysql4_Rule_Customer_Collection
  • Mage_SalesRule_Model_Mysql4_Rule_Product
  • Mage_SalesRule_Model_Mysql4_Rule_Product_Collection
  • Mage_SalesRule_Model_Observer
  • Mage_SalesRule_Model_Quote_Discount
  • Mage_SalesRule_Model_Quote_Freeshipping
  • Mage_SalesRule_Model_Quote_Nominal_Discount
  • Mage_SalesRule_Model_Resource_Coupon
  • Mage_SalesRule_Model_Resource_Coupon_Collection
  • Mage_SalesRule_Model_Resource_Coupon_Usage
  • Mage_SalesRule_Model_Resource_Report_Collection
  • Mage_SalesRule_Model_Resource_Report_Rule
  • Mage_SalesRule_Model_Resource_Report_Rule_Createdat
  • Mage_SalesRule_Model_Resource_Report_Rule_Updatedat
  • Mage_SalesRule_Model_Resource_Report_Updatedat_Collection
  • Mage_SalesRule_Model_Resource_Rule
  • Mage_SalesRule_Model_Resource_Rule_Collection
  • Mage_SalesRule_Model_Resource_Rule_Customer
  • Mage_SalesRule_Model_Resource_Rule_Customer_Collection
  • Mage_SalesRule_Model_Resource_Rule_Product
  • Mage_SalesRule_Model_Resource_Rule_Product_Collection
  • Mage_SalesRule_Model_Rule
  • Mage_SalesRule_Model_Rule_Action_Collection
  • Mage_SalesRule_Model_Rule_Action_Product
  • Mage_SalesRule_Model_Rule_Condition_Address
  • Mage_SalesRule_Model_Rule_Condition_Combine
  • Mage_SalesRule_Model_Rule_Condition_Product
  • Mage_SalesRule_Model_Rule_Condition_Product_Combine
  • Mage_SalesRule_Model_Rule_Condition_Product_Found
  • Mage_SalesRule_Model_Rule_Condition_Product_Subselect
  • Mage_SalesRule_Model_Rule_Customer
  • Mage_SalesRule_Model_Rule_Product
  • Mage_SalesRule_Model_System_Config_Source_Coupon_Format
  • Mage_SalesRule_Model_Validator

Interfaces

  • Mage_SalesRule_Model_Coupon_CodegeneratorInterface
  • 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_SalesRule
 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: class Mage_SalesRule_Model_Observer
 29: {
 30:     protected $_validator;
 31: 
 32:     /**
 33:      * Get quote item validator/processor object
 34:      *
 35:      * @deprecated
 36:      * @param   Varien_Event $event
 37:      * @return  Mage_SalesRule_Model_Validator
 38:      */
 39:     public function getValidator($event)
 40:     {
 41:         if (!$this->_validator) {
 42:             $this->_validator = Mage::getModel('salesrule/validator')
 43:                 ->init($event->getWebsiteId(), $event->getCustomerGroupId(), $event->getCouponCode());
 44:         }
 45:         return $this->_validator;
 46:     }
 47: 
 48:     /**
 49:      * Process quote item (apply discount to item)
 50:      *
 51:      * @deprecated process call movet to total model
 52:      * @param Varien_Event_Observer $observer
 53:      */
 54:     public function sales_quote_address_discount_item($observer)
 55:     {
 56:         $this->getValidator($observer->getEvent())
 57:             ->process($observer->getEvent()->getItem());
 58:     }
 59: 
 60:     public function sales_order_afterPlace($observer)
 61:     {
 62:         $order = $observer->getEvent()->getOrder();
 63: 
 64:         if (!$order) {
 65:             return $this;
 66:         }
 67: 
 68:         // lookup rule ids
 69:         $ruleIds = explode(',', $order->getAppliedRuleIds());
 70:         $ruleIds = array_unique($ruleIds);
 71: 
 72:         $ruleCustomer = null;
 73:         $customerId = $order->getCustomerId();
 74: 
 75:         // use each rule (and apply to customer, if applicable)
 76:         foreach ($ruleIds as $ruleId) {
 77:             if (!$ruleId) {
 78:                 continue;
 79:             }
 80:             $rule = Mage::getModel('salesrule/rule');
 81:             $rule->load($ruleId);
 82:             if ($rule->getId()) {
 83:                 $rule->setTimesUsed($rule->getTimesUsed() + 1);
 84:                 $rule->save();
 85: 
 86:                 if ($customerId) {
 87:                     $ruleCustomer = Mage::getModel('salesrule/rule_customer');
 88:                     $ruleCustomer->loadByCustomerRule($customerId, $ruleId);
 89: 
 90:                     if ($ruleCustomer->getId()) {
 91:                         $ruleCustomer->setTimesUsed($ruleCustomer->getTimesUsed()+1);
 92:                     }
 93:                     else {
 94:                         $ruleCustomer
 95:                         ->setCustomerId($customerId)
 96:                         ->setRuleId($ruleId)
 97:                         ->setTimesUsed(1);
 98:                     }
 99:                     $ruleCustomer->save();
100:                 }
101:             }
102:         }
103: 
104:         $coupon = Mage::getModel('salesrule/coupon');
105:         /** @var Mage_SalesRule_Model_Coupon */
106:         $coupon->load($order->getCouponCode(), 'code');
107:         if ($coupon->getId()) {
108:             $coupon->setTimesUsed($coupon->getTimesUsed() + 1);
109:             $coupon->save();
110:             if ($customerId) {
111:                 $couponUsage = Mage::getResourceModel('salesrule/coupon_usage');
112:                 $couponUsage->updateCustomerCouponTimesUsed($customerId, $coupon->getId());
113:             }
114:         }
115:     }
116: 
117:     /**
118:      * Refresh sales coupons report statistics for last day
119:      *
120:      * @param Mage_Cron_Model_Schedule $schedule
121:      * @return Mage_SalesRule_Model_Observer
122:      */
123:     public function aggregateSalesReportCouponsData($schedule)
124:     {
125:         Mage::app()->getLocale()->emulate(0);
126:         $currentDate = Mage::app()->getLocale()->date();
127:         $date = $currentDate->subHour(25);
128:         Mage::getResourceModel('salesrule/report_rule')->aggregate($date);
129:         Mage::app()->getLocale()->revert();
130:         return $this;
131:     }
132: 
133:     /**
134:      * Check rules that contains affected attribute
135:      * If rules were found they will be set to inactive and notice will be add to admin session
136:      *
137:      * @param string $attributeCode
138:      * @return Mage_SalesRule_Model_Observer
139:      */
140:     protected function _checkSalesRulesAvailability($attributeCode)
141:     {
142:         /* @var $collection Mage_SalesRule_Model_Mysql4_Rule_Collection */
143:         $collection = Mage::getResourceModel('salesrule/rule_collection')
144:             ->addAttributeInConditionFilter($attributeCode);
145: 
146:         $disabledRulesCount = 0;
147:         foreach ($collection as $rule) {
148:             /* @var $rule Mage_SalesRule_Model_Rule */
149:             $rule->setIsActive(0);
150:             /* @var $rule->getConditions() Mage_SalesRule_Model_Rule_Condition_Combine */
151:             $this->_removeAttributeFromConditions($rule->getConditions(), $attributeCode);
152:             $this->_removeAttributeFromConditions($rule->getActions(), $attributeCode);
153:             $rule->save();
154: 
155:             $disabledRulesCount++;
156:         }
157: 
158:         if ($disabledRulesCount) {
159:             Mage::getSingleton('adminhtml/session')->addWarning(
160:                 Mage::helper('salesrule')->__('%d Shopping Cart Price Rules based on "%s" attribute have been disabled.', $disabledRulesCount, $attributeCode));
161:         }
162: 
163:         return $this;
164:     }
165: 
166:     /**
167:      * Remove catalog attribute condition by attribute code from rule conditions
168:      *
169:      * @param Mage_Rule_Model_Condition_Combine $combine
170:      * @param string $attributeCode
171:      */
172:     protected function _removeAttributeFromConditions($combine, $attributeCode)
173:     {
174:         $conditions = $combine->getConditions();
175:         foreach ($conditions as $conditionId => $condition) {
176:             if ($condition instanceof Mage_Rule_Model_Condition_Combine) {
177:                 $this->_removeAttributeFromConditions($condition, $attributeCode);
178:             }
179:             if ($condition instanceof Mage_SalesRule_Model_Rule_Condition_Product) {
180:                 if ($condition->getAttribute() == $attributeCode) {
181:                     unset($conditions[$conditionId]);
182:                 }
183:             }
184:         }
185:         $combine->setConditions($conditions);
186:     }
187: 
188:     /**
189:      * After save attribute if it is not used for promo rules already check rules for containing this attribute
190:      *
191:      * @param Varien_Event_Observer $observer
192:      * @return Mage_SalesRule_Model_Observer
193:      */
194:     public function catalogAttributeSaveAfter(Varien_Event_Observer $observer)
195:     {
196:         $attribute = $observer->getEvent()->getAttribute();
197:         if ($attribute->dataHasChangedFor('is_used_for_promo_rules') && !$attribute->getIsUsedForPromoRules()) {
198:             $this->_checkSalesRulesAvailability($attribute->getAttributeCode());
199:         }
200: 
201:         return $this;
202:     }
203: 
204:     /**
205:      * After delete attribute check rules that contains deleted attribute
206:      * If rules was found they will seted to inactive and added notice to admin session
207:      *
208:      * @param Varien_Event_Observer $observer
209:      * @return Mage_SalesRule_Model_Observer
210:      */
211:     public function catalogAttributeDeleteAfter(Varien_Event_Observer $observer)
212:     {
213:         $attribute = $observer->getEvent()->getAttribute();
214:         if ($attribute->getIsUsedForPromoRules()) {
215:             $this->_checkSalesRulesAvailability($attribute->getAttributeCode());
216:         }
217: 
218:         return $this;
219:     }
220: 
221:     /**
222:      * Append sales rule product attributes to select by quote item collection
223:      *
224:      * @param Varien_Event_Observer $observer
225:      * @return Mage_SalesRule_Model_Observer
226:      */
227:     public function addProductAttributes(Varien_Event_Observer $observer)
228:     {
229:         // @var Varien_Object
230:         $attributesTransfer = $observer->getEvent()->getAttributes();
231: 
232:         $attributes = Mage::getResourceModel('salesrule/rule')
233:             ->getActiveAttributes(
234:                 Mage::app()->getWebsite()->getId(),
235:                 Mage::getSingleton('customer/session')->getCustomer()->getGroupId()
236:             );
237:         $result = array();
238:         foreach ($attributes as $attribute) {
239:             $result[$attribute['attribute_code']] = true;
240:         }
241:         $attributesTransfer->addData($result);
242:         return $this;
243:     }
244: 
245:     /**
246:      * Add coupon's rule name to order data
247:      *
248:      * @param Varien_Event_Observer $observer
249:      * @return Mage_SalesRule_Model_Observer
250:      */
251:     public function addSalesRuleNameToOrder($observer)
252:     {
253:         $order = $observer->getOrder();
254:         $couponCode = $order->getCouponCode();
255: 
256:         if (empty($couponCode)) {
257:             return $this;
258:         }
259: 
260:         /**
261:          * @var Mage_SalesRule_Model_Coupon $couponModel
262:          */
263:         $couponModel = Mage::getModel('salesrule/coupon');
264:         $couponModel->loadByCode($couponCode);
265: 
266:         $ruleId = $couponModel->getRuleId();
267: 
268:         if (empty($ruleId)) {
269:             return $this;
270:         }
271: 
272:         /**
273:          * @var Mage_SalesRule_Model_Rule $ruleModel
274:          */
275:         $ruleModel = Mage::getModel('salesrule/rule');
276:         $ruleModel->load($ruleId);
277: 
278:         $order->setCouponRuleName($ruleModel->getName());
279: 
280:         return $this;
281:     }
282: }
283: 
284: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0