Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <controller_action_postdispatch_checkout_onepage_saveBilling><!-- when a billing address is saved check for double discounts -->
  2. <observers>
  3. <yourmodule_observer_check_double_discount>
  4. <type>singleton</type>
  5. <class>namespace_yourmodule/observer</class>
  6. <method>checkForDoubleDiscount</method>
  7. </yourmodule_observer_check_double_discount>
  8. </observers>
  9. </controller_action_postdispatch_checkout_onepage_saveBilling>
  10.  
  11. public function checkForDoubleDiscount($observer) {
  12.  
  13. $sess = Mage::getSingleton('checkout/session');
  14. $quote = $sess->getQuote();
  15.  
  16. //check if quote has discount
  17. $coupon_code = $quote->getCouponCode();
  18. if($coupon_code){
  19. //check if email address is set
  20. if($email = $quote->getCustomerEmail()) {
  21.  
  22. //get salesrule
  23. $coupon = Mage::getModel('salesrule/coupon')->load($coupon_code, 'code');
  24. $rule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId());
  25.  
  26. //check if usage limit exists
  27. if($maxuse = $rule->getUsesPerCustomer()) {
  28.  
  29. //get all orders of the customer
  30. $order = Mage::getModel('sales/order');
  31. $collection = $order->getCollection()
  32. ->addAttributeToFilter('customer_email',array('like'=>$email))
  33. ->addAttributeToFilter('coupon_code',array('like'=>$coupon_code));
  34. $count = 0;
  35. foreach($collection as $o){
  36. //check if coupon code has been applied
  37. if(strtolower($o->getCouponCode()) == strtolower($coupon_code)) $count ++;
  38. //if max usage is reached, remove rule from quote
  39. if($maxuse <= $collection->count()) {
  40. $quote->setCouponCode('')->collectTotals()->save();
  41. $message = Mage::helper('namespace_yourmodule');->__('Coupon Code %s has been cancelled, the usage limit has been reached', $coupon_code);
  42. $result = array();
  43. $result['error'] = '-1';
  44. $result['message'] = $message;
  45. print Mage::helper('core')->jsonEncode($result); exit;
  46. break;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement