开发者

Magento: setCouponCode does not seem to apply coupon on quote model

we are using Magento mainly for transactions and have rewritten the frontend entirely through a custom application. I am having trouble applying a coupon code (shopping cart price rule) on a quote object. The coupon code seems to be rejected - setCouponCode does not return any error, but getCouponCode returns empty string.

I have verifi开发者_运维问答ed that the coupon code is valid by making a transacting through the admin backend. Here is the code snippet below.

Can someone help me with getting the quote model object to accept and apply a coupon code?

function add_coupon($shoppingCartId, $couponcode) {

    try {
            $quoteObj = Mage::getModel('sales/quote')->load($shoppingCartId);
            $quoteObj->getShippingAddress()->setCollectShippingRates(true);
            $quoteObj->getShippingAddress()->setCouponCode($coupon)
                    ->setTotalsCollectedFlag(true)
                    ->collectTotals()
                    ->save();
    } catch (Exception $e) {
            return array("status"=>"failed", "message"=>"Error applying coupon.");
    }
    if ($coupon) {
            if (!$coupon == $quoteObj->getCouponCode()) {
                    return array("status"=>"failed", "message"=>"Coupon code is not valid.");
            }
    }
    return array("status"=>"success");

}


I ran into the same issue, and discovered that I needed to call setCouponCode() before adding any items to my quote.

In your case, that would look like:

$quoteObj = Mage::getModel('sales/quote')->setCouponCode($coupon)->load($shoppingCartId);


All logic needed for dealing with coupons is in SalesRule module.

There is model Mage_SalesRule_Model_Coupon which is the coupon object, and it has a resource model Mage_SalesRule_Model_Mysql4_Coupon.

To create a new coupon you could instantiate the above coupon model, fill all fields and call save() method. It will write data to table salesrule_coupon. But if you'll look at this table you'll see that coupons depend on rule id, so you need to create some sales rule first.

Mage_SalesRule_Model_Rule cares about rules, it also has own resource model. I think it will be helpful for you to investigate how they are works


I lost a few hours to this tonight. Hopefully I can save someone the same. Emily is correct for this example (where you load the shopping cart in to a quote).

This applies to Magento 1.7.0.2, I'm not sure if it holds for other versions.

If you are programmatically creating an order but you are not using the shopping cart to do so this may help you: The underlying problem is that Magento is running collectTotals() each time you add/update an address on the quote - and here's the important part: it caches them. (In fact, after you save an address if you check the getTotalsCollectedFlag you'll see it's set to true!). Setting the coupon code after this, even if you run collectTotals(), doesn't apply the coupon code.

You could call setTotalsCollectedFlag(false) on the quote then apply the coupon, but this is a bad idea (it could cause some miscalculations to occur according to a Magento bug report that is no longer accessible), what you want to do is call setCouponCode before you set any addresses (or at the very least, before you set the last address).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜