2016-07-13 95 views
0
public function coupon($data) { 
     $couponCode = $data['couponcode']; 
     if (!Zend_Validate::is(trim($couponCode), 'NotEmpty')) { 
      throw new Exception($this->__('coupon code cannot be empty.')); 
     } 
     $oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode, 'code'); 
     $data = $oCoupon->getData(); 
     if (empty($data)) { 
      throw new Exception($this->__('coupon code did not match.')); 
     }  
     $quote = Mage::getModel('checkout/session')->getQuote(); 
     $quote->setCouponCode($couponCode); 
     $quote->save(); 
     $quoteData = Mage::getModel('checkout/cart')->getQuote(); 
     $subTotal = $quoteData['subtotal']; 
     $subtotal_with_discount = $quoteData['subtotal_with_discount']; 
     $grandTotal = $quoteData['grand_total']; 
     $discountTotal = ($subTotal - $subtotal_with_discount); 
     $discount = number_format($discountTotal, 4, null, ''); 
     return $discount; 
    } 

優惠券代碼應用和展示,但是當我打印quoteData->的GetData(),那麼折扣不來了,到時我會更新刷新車頁面,然後貼現來了通過優惠券代碼添加折扣編程

回答

2

我得到了解決

public function coupon($data) { 
     $couponCode = $data['couponcode']; 
     if (!Zend_Validate::is(trim($couponCode), 'NotEmpty')) { 
      throw new Exception($this->__('coupon code cannot be empty.')); 
     } 
     $oCoupon = Mage::getModel('salesrule/coupon')->load($couponCode, 'code'); 
     $data = $oCoupon->getData(); 
     if (empty($data)) { 
      throw new Exception($this->__('coupon code did not match.')); 
     } 
     Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress() 
       ->setCollectShippingRates(true); 
     Mage::getSingleton('checkout/cart')->getQuote() 
       ->setCouponCode($couponCode)->collectTotals()->save(); 
     $quoteData = Mage::getModel('checkout/cart')->getQuote(); 
     $subTotal = $quoteData['subtotal']; 
     $subtotal_with_discount = $quoteData['subtotal_with_discount']; 
     $grandTotal = $quoteData['grand_total']; 
     $discountTotal = ($subTotal - $subtotal_with_discount); 
     $discount = number_format($discountTotal, 4, null, ''); 

     return $discount; 
    } 
相關問題