/**
  * Add coupon to cart
  */
 public function postAction()
 {
     $quote = $this->_getQuote();
     if ($quote->getItemsCount()) {
         try {
             $couponCode = (string) $this->getRequest()->getParam('coupon_code');
             $oldCouponCode = $quote->getCouponCode();
             $this->couponService->addCoupon($quote, $couponCode);
             $newCouponCode = $quote->getCouponCode();
             $foundInOldCoupon = stristr($oldCouponCode, $couponCode) !== false;
             $foundInNewCoupon = stristr($newCouponCode, $couponCode) !== false;
             $hasCouponChanged = $foundInOldCoupon === false && $foundInNewCoupon === true;
             if ($hasCouponChanged) {
                 $this->_getSession()->addSuccess($this->__('Coupon code "%s" was applied.', Mage::helper('core')->escapeHtml($couponCode)));
             } else {
                 $this->_getSession()->addError($this->__('Coupon code "%s" is not valid.', Mage::helper('core')->escapeHtml($couponCode)));
             }
         } catch (Mage_Core_Exception $e) {
             $this->_getSession()->addError($e->getMessage());
         } catch (Exception $e) {
             $this->_getSession()->addError($this->__('Cannot apply the coupon code.'));
             Mage::logException($e);
         }
     }
     $this->_goBack();
 }
Esempio n. 2
0
 /**
  * @param Varien_Event_Observer $observer
  *  this observer event contains:
  *      - 'order_create_model'  => Mage_Adminhtml_Model_Sales_Order_Create
  *      - 'request_model'       => Mage_Core_Controller_Request_Http
  *      - 'session'             => Mage_Adminhtml_Model_Session_Quote
  */
 public function handleAdminCouponAdd(Varien_Event_Observer $observer)
 {
     $event = $observer->getEvent();
     /** @var Mage_Core_Controller_Request_Http $request */
     $request = $event->getRequestModel();
     $order = $request->getPost('order');
     if (isset($order['coupon']['code'])) {
         /** @var Mage_Adminhtml_Model_Session_Quote $session */
         $session = $event->getSession();
         $quote = $session->getQuote();
         if (isset($order['coupon']['remove'])) {
             $this->couponService->removeCoupon($quote, $order['coupon']['code']);
         } else {
             $this->couponService->addCoupon($quote, $order['coupon']['code']);
         }
         // Remove coupon from the request.
         // - see Mage_Adminhtml_Model_Sales_Order_Create::importPostData
         unset($order['coupon']);
         $request->setPost('order', $order);
     }
 }