Question

I've been struggling with this for a while, should be pretty straight forward but... I'm trying to apply coupon to order using magento cart.coupon.add api. The product is Virtual here is my code ( and I've tried everything i could find on google before I came here ):

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;
}

I've also tried applying coupon to address:

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);
}

the coupon is valid but after the order being placed in the Magento admin I see that the discount amount = 0.0 and the user was charged full amount to his credit card. Anyone....? Help...?

Was it helpful?

Solution

Finally found an answer

I needed to call setCouponCode() before adding any items to quote.

$quote= Mage::getModel('sales/quote')->setCouponCode($couponCode);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top