문제

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