Guest User

Untitled

a guest
Jan 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. <?php
  2. require_once Mage::getModuleDir('controllers', "Mage_Checkout").DS."IndexController.php";
  3. class Amit_CustomCheckout_OnepageController extends Mage_Checkout_OnepageController
  4. {
  5. public function applycouponAction(){ /**
  6. * No reason continue with empty shopping cart
  7. */
  8. if (!$this->getOnepage()->getQuote()->getItemsCount()) {
  9. $result['error']=true;
  10. $result['message']=$this->__('No item in cart.');
  11. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  12.  
  13. return;
  14. }
  15.  
  16. $couponCode = (string) $this->getRequest()->getParam('coupon_code');
  17. if ($this->getRequest()->getParam('remove') == 1) {
  18. $couponCode = '';
  19. }
  20. $oldCouponCode = $this->getOnepage()->getQuote()->getCouponCode();
  21.  
  22. if (!strlen($couponCode) && !strlen($oldCouponCode)) {
  23. $result['error']=true;
  24. $result['message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
  25. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  26. return;
  27. }
  28.  
  29. try {
  30. $codeLength = strlen($couponCode);
  31. $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;
  32.  
  33. $this->getOnepage()->getQuote()->getShippingAddress()->setCollectShippingRates(true);
  34. $this->getOnepage()->getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')
  35. ->collectTotals()
  36. ->save();
  37.  
  38. $result=array();
  39.  
  40. if ($codeLength) {
  41. if ($isCodeLengthValid && $couponCode == $this->getOnepage()->getQuote()->getCouponCode()) {
  42. $result['error']=false;
  43. $result['message']=$this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode));
  44. $result['html']=$this->_getReviewHtml();
  45. } else {
  46. $result['error']=true;
  47. $result['message']=$this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode));
  48. }
  49. } else {
  50.  
  51. $result['error']=false;
  52. $result['message']=$this->__('Coupon code was canceled.');
  53. $result['html']=$this->_getReviewHtml();
  54.  
  55. }
  56.  
  57. } catch (Mage_Core_Exception $e) {
  58. $this->_getSession()->addError($e->getMessage());
  59. $result['error']=true;
  60. $result['message']=$this->__('Cannot apply the coupon code.');
  61.  
  62. } catch (Exception $e) {
  63. $result['error']=true;
  64. $result['message']=$this->__('Cannot apply the coupon code.');
  65. Mage::logException($e);
  66. }
  67. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  68.  
  69. }
  70. }
Add Comment
Please, Sign In to add comment