Created
June 2, 2015 10:34
-
-
Save peterjaap/bf9aa2086f06a1a41fa1 to your computer and use it in GitHub Desktop.
Fixed Orchid_CouponFix_Model_Observer for Magento 1.7+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);