Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <controller_action_postdispatch_checkout_onepage_saveBilling><!-- when a billing address is saved check for double discounts -->
- <observers>
- <yourmodule_observer_check_double_discount>
- <type>singleton</type>
- <class>namespace_yourmodule/observer</class>
- <method>checkForDoubleDiscount</method>
- </yourmodule_observer_check_double_discount>
- </observers>
- </controller_action_postdispatch_checkout_onepage_saveBilling>
- public function checkForDoubleDiscount($observer) {
- $sess = Mage::getSingleton('checkout/session');
- $quote = $sess->getQuote();
- //check if quote has discount
- $coupon_code = $quote->getCouponCode();
- if($coupon_code){
- //check if email address is set
- if($email = $quote->getCustomerEmail()) {
- //get salesrule
- $coupon = Mage::getModel('salesrule/coupon')->load($coupon_code, 'code');
- $rule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId());
- //check if usage limit exists
- if($maxuse = $rule->getUsesPerCustomer()) {
- //get all orders of the customer
- $order = Mage::getModel('sales/order');
- $collection = $order->getCollection()
- ->addAttributeToFilter('customer_email',array('like'=>$email))
- ->addAttributeToFilter('coupon_code',array('like'=>$coupon_code));
- $count = 0;
- foreach($collection as $o){
- //check if coupon code has been applied
- if(strtolower($o->getCouponCode()) == strtolower($coupon_code)) $count ++;
- //if max usage is reached, remove rule from quote
- if($maxuse <= $collection->count()) {
- $quote->setCouponCode('')->collectTotals()->save();
- $message = Mage::helper('namespace_yourmodule');->__('Coupon Code %s has been cancelled, the usage limit has been reached', $coupon_code);
- $result = array();
- $result['error'] = '-1';
- $result['message'] = $message;
- print Mage::helper('core')->jsonEncode($result); exit;
- break;
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement