I'm integrating a system with Magento.

When I create an order, if my non-Magento system indicates the order does have a coupon I would like to mark the coupon code as "used" for that customer, so the next time he tries to use it, Magento will know that customer already used it.

How can I accomplish this?

有帮助吗?

解决方案

I manage to do it like this:

 $coupon = mage::getModel('salesrule/coupon')->load($code, 'code');
 $coupon->setTimesUsed($coupon->getTimesUsed()+1);
 $coupon->save();

 $rule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId());
 $rule->setTimesUsed($rule->getTimesUsed()+1);
 $rule->save();


 $couponUsage = Mage::getResourceModel('salesrule/coupon_usage');
 $couponUsage->updateCustomerCouponTimesUsed($customerId,$coupon->getCouponId());

Thanks to this post: Magento - Single Coupon marked as used when payment pending

其他提示

Set the Shopping Cart Rule's Uses per Customer to 1.

----EDIT----

If you want to apply the coupon in your code then you can do something like this:

$coupon_code = "YOUR_CODE";

Mage::getSingleton('checkout/cart')
    ->getQuote()
    ->setCouponCode($coupon_code)
    ->collectTotals()
    ->save();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top