Frage

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?

War es hilfreich?

Lösung

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

Andere Tipps

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();
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top