Skip to content

Instantly share code, notes, and snippets.

@RabeaWahab
Created August 31, 2015 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RabeaWahab/c065d478a898c5a454f9 to your computer and use it in GitHub Desktop.
Save RabeaWahab/c065d478a898c5a454f9 to your computer and use it in GitHub Desktop.
public function retrieveCoupon($observer)
{
$payment = $observer->getEvent()->getPayment();
$order = $payment->getOrder();
if ($code = $order->getCouponCode()) {
$coupon = Mage::getModel('salesrule/coupon')->load($code, 'code');
$coupon->setTimesUsed($coupon->getTimesUsed()-1);
$coupon->save();
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$query = "UPDATE salesrule_coupon_usage SET times_used = times_used-1 WHERE customer_id='".$order->getCustomerId()."' AND coupon_id='".$coupon->getId()."'";
$result = $write->query($query);
if($customerId = $order->getCustomerId()) {
if ($customerCoupon = Mage::getModel('salesrule/rule_customer')->loadByCustomerRule($customerId, $coupon->getRuleId())) {
$customerCoupon->setTimesUsed($customerCoupon->getTimesUsed()-1);
$customerCoupon->save();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment