2012-09-09 100 views
0

我一直在努力與此一段時間,應該是相當直接,但... 我試圖使用magento cart.coupon.add API訂單優惠券。 該產品是虛擬 這裏是我的代碼(和我用盡了一切我可以在谷歌找到我來這裏之前):Magento核心折扣

protected function _applyCoupon($quoteId, $couponCode, $store = null) 
{ 
    $coupon = Mage::getModel('salesrule/coupon'); 
    $coupon->loadByCode($couponCode); 
    Mage::log('_applyCoupon('.$couponCode.')'); 
    $quote = $this->_getQuote($quoteId, $store); 

    if (!$quote->getItemsCount()) { 
    //  $this->_fault('quote_is_empty'); 
    } 

    $oldCouponCode = $quote->getCouponCode(); 
    if (!strlen($couponCode) && !strlen($oldCouponCode)) { 
     return false; 
    } 
    try { 
     //$quote->getShippingAddress()->setCollectShippingRates(true); 
     $quote->setCouponCode($couponCode); 
     $quote->setTotalsCollectedFlag(false)->collectTotals(); 
     $quote->collectTotals(); 
     $quote->save(); 
     Mage::getModel("checkout/session")->setData("coupon_code",$couponCode); 
     Mage::getModel('checkout/cart')->getQuote()->setCouponCode($couponCode)->save(); 
     Mage::getModel('checkout/cart')->getQuote()->collectTotals(); 
     Mage::getModel('checkout/cart')->getQuote()->save(); 
     Mage::log("_applyCoupon : Set coupon to quote:".$quote->getCouponCode()); 

    } catch (Exception $e) { 
     $this->_fault("cannot_apply_coupon_code", $e->getMessage()); 
    } 
     Mage::log('3'); 

    if ($couponCode) { 
     Mage::log("Coupon applied"); 
     if (!$couponCode == $quote->getCouponCode()) { 
      Mage::log('3.2'); 
      $this->_fault('coupon_code_is_not_valid'); 
     } 
    } 

    return true; 
} 

我也試着申請優惠券來解決:

protected function applyDiscountToAddress($address,$quote) 
{ 
    Mage::log('applyDiscountToProduct ...'); 
    $coupon = Mage::getModel('salesrule/coupon'); 
    Mage::log("checkoutprocess: checkout/session:".Mage::getModel("checkout/session")->getData("coupon_code")); 
    $coupon->loadByCode(Mage::getModel("checkout/session")->getData("coupon_code")); 
    $rule = Mage::getModel('salesrule/rule'); 
    $rule->load($coupon->getRuleId()); 
    $discountamount = $rule->getDiscountAmount(); 
    $dbldiscount = 0.00 + $discountamount; 
    $grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal(); 
    $subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal(); 
    Mage::log('applyDiscountToProduct $grandTotal:'.$grandTotal); 
    Mage::log('applyDiscountToProduct $subTotal:'.$subTotal); 

    $gTotal = $grandTotal - $dbldiscount; 
    $address->setDiscountAmount($dbldiscount) 
     ->setBaseDiscountAmount($dbldiscount) 
     ->setGrandTotal($gTotal) 
     ->setBaseGrandTotal($gTotal); 

    $grandTotal  = $address->getGrandTotal(); 
    $baseGrandTotal = $address->getBaseGrandTotal(); 
    Mage::log('applyDiscountToProduct Address:$grandTotal:'.$grandTotal); 
    Mage::log('applyDiscountToProduct Address:$baseGrandTotal:'.$baseGrandTotal); 

    $totals  = array_sum($address->getAllTotalAmounts()); 
    $baseTotals = array_sum($address->getAllBaseTotalAmounts()); 

    $address->setGrandTotal($grandTotal+$totals); 
    $address->setBaseGrandTotal($baseGrandTotal+$baseTotals); 
} 

優惠券是有效的,但在訂單被放置在Magento管理員之後,我看到折扣金額= 0.0,並且用戶從他的信用卡中收取了全部金額。 任何人....?幫幫我...?

回答

0

終於找到了答案

我需要添加任何產品報價之前調用setCouponCode()

$quote= Mage::getModel('sales/quote')->setCouponCode($couponCode); 
+0

難道你們兩個能詳細說明這個答案嗎?我目前在使用Magento 1.9中的虛擬產品時有類似的問題。謝謝! – Dan

+0

我們是否應該在加載付款時已經創建的報價或者第一次創建購物車/報價時這樣做,在這種情況下用戶不會添加優惠券,而是在購物車中添加物品。 –