Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created June 2, 2015 10:34
Show Gist options
  • Select an option

  • Save peterjaap/bf9aa2086f06a1a41fa1 to your computer and use it in GitHub Desktop.

Select an option

Save peterjaap/bf9aa2086f06a1a41fa1 to your computer and use it in GitHub Desktop.
Fixed Orchid_CouponFix_Model_Observer for Magento 1.7+
<?php
class Orchid_CouponFix_Model_Observer
{
public function cancel($observer)
{
$resource = Mage::getModel('core/resource');
$db = $resource->getConnection('core_write');
$event = $observer->getEvent();
$order = $event->getPayment()->getOrder();
if ($order->canCancel()) {
if ($code = $order->getCouponCode()) {
try {
$coupon = Mage::getModel('salesrule/coupon')->load($code, 'code');
$coupon->setTimesUsed($coupon->getTimesUsed() - 1);
$coupon->save();
} catch(Exception $e) {
Mage::log('Could not decrease usage by 1 for coupon ' .$code . '; ' . $e->getMessage());
return;
}
try {
$couponRule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId(), 'rule_id');
$couponRule->setTimesUsed($couponRule->getTimesUsed() - 1);
$couponRule->save();
} catch(Exception $e) {
Mage::log('Could not decrease usage by 1 for coupon rule ' .$coupon->getRuleId(). '; ' . $e->getMessage());
}
$customerId = $order->getCustomerId();
if($customerId && $couponRule) {
try {
$customerCoupon = Mage::getModel('salesrule/rule_customer')->loadByCustomerRule($customerId, $couponRule->getId());
if ($customerCoupon) {
$customerCoupon->setTimesUsed($customerCoupon->getTimesUsed() - 1);
$customerCoupon->save();
}
} catch(Exception $e) {
Mage::log('Could not decrease usage by 1 for customer coupon ' . $code . ' / ' . $coupon->getId() . ' / ' . $customerId . '; ' . $e->getMessage());
}
try {
$couponUsage = $db->fetchOne($db->select()->from('salesrule_coupon_usage','times_used')->where('customer_id = ?', $customerId)->where('coupon_id = ?', $coupon->getId()));
$where = array(
$db->quoteInto('coupon_id = ?', $coupon->getId()),
$db->quoteInto('customer_id = ?', $customerId)
);
if($couponUsage <= 1) {
$db->delete('salesrule_coupon_usage', $where);
} else {
$db->update('salesrule_coupon_usage', array('times_used' => $couponUsage - 1), $where);
}
} catch(Exception $e) {
Mage::log('Could not decrease usage by 1 for customer coupon usage ' . $code . ' / ' . $coupon->getId() . ' / ' . $customerId . '; ' . $e->getMessage());
}
}
}
}
}
}
@Messa1
Copy link
Copy Markdown

Messa1 commented Mar 8, 2016

I have the same question. Did this code works for a 1.9.X Version?

@holykim66
Copy link
Copy Markdown

This code works great with Magento 1.9.3. Just replace Observer.php with this code from http://magebase.com/magento-tutorials/quick-fix-coupon-codes-used-up-on-incomplete-transactions/

Thanks Peter Jaap!

@webdreamsnc
Copy link
Copy Markdown

Someone can use db table prefix and the queries used into example above will not work.
You have to change all the query syntax.
For example:
$db->delete('salesrule_coupon_usage', $where);
change to:
$db->delete($resource->getTableName('salesrule_coupon_usage'), $where);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment