we have custom module with following files.
etc/config.xml
<config>
<modules>
<Magetechno_CancelCouponcode>
<version>0.1.0</version>
</Magetechno_CancelCouponcode>
</modules>
<global>
<models>
<cancelcouponcode>
<class>Magetechno_CancelCouponcode_Model</class>
</cancelcouponcode>
</models>
<events>
<sales_order_payment_cancel>
<observers>
<cancelcouponcode_observer>
<type>singleton</type>
<class>Magetechno_CancelCouponcode_Model_Observer</class>
<method>cancel</method>
</cancelcouponcode_observer>
</observers>
</sales_order_payment_cancel>
</events>
</global>
</config>
model/observer.php
<?php
class Magetechno_CancelCouponcode_Model_Observer
{
public function cancel($observer)
{
$event = $observer->getEvent();
$order = $event->getPayment()->getOrder();
if ($order->canCancel()) {
if ($code = $order->getCouponCode()) {
$coupon = mage::getModel('salesrule/coupon')->load($code,'code');
if ($coupon->getTimesUsed() > 0) {
$coupon->setTimesUsed($coupon->getTimesUsed()-1);
$coupon->save();
}
$rule = Mage::getModel('salesrule/rule')->load($coupon->getRuleId());
if ($rule->getTimesUsed() > 0) {
$rule->setTimesUsed($rule->getTimesUsed()-1);
$rule->save();
}
if($customerId = $order->getCustomerId()) {
if ($customerCoupon = Mage::getModel('salesrule/rule_customer')->loadByCustomerRule($customerId, $rule->getId())) {
$couponUsage = new Varien_Object();
Mage::getResourceModel('salesrule/coupon_usage')->loadByCustomerCoupon($couponUsage, $customerId, $coupon->getId());
if ($couponUsage->getTimesUsed() > 0) {
/* I can't find any #@$!@$ interface to do anything but increment a coupon_usage record */
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$tableName = $resource->getTableName('salesrule_coupon_usage');
Mage::log($couponUsage->getTimesUsed());
Mage::log("Coupon used time above");
if($couponUsage->getTimesUsed()==1)
{
$__condition1 = array($writeConnection->quoteInto('coupon_id=?',$coupon->getId()));
$__condition1 = array($writeConnection->quoteInto('customer_id=?',$customerId));
if($writeConnection->delete('salesrule_coupon_usage', $__condition1))
Mage::log("Its deleted from salesrule_coupon_usage");
$__condition2 = array($writeConnection->quoteInto('rule_id=?',$rule->getId()));
$__condition2 = array($writeConnection->quoteInto('customer_id=?',$customerId));
if($writeConnection->delete('salesrule_customer', $__condition2))
Mage::log("Its deleted from salesrule_custome");
}
else
{
$query = "UPDATE {$tableName} SET times_used = times_used-1 " .
" WHERE coupon_id = {$coupon->getId()} AND customer_id = {$customerId} AND times_used > 0";
$writeConnection->query($query);
Mage::log("We have updated it");
}
}
if ($customerCoupon->getTimesUsed() > 0) {
$customerCoupon->setTimesUsed($customerCoupon->getTimesUsed()-1);
$customerCoupon->save();
//Mage::log('setTimesUsed customer saved Rule Time used');
}
}
}
}
}
}
}
but we dont have app/etc/module_name/module.xml file. we created as below. but we dont know what name to give instead of Module_name text in below file to get the result of the module.
app/etc/modules/Module_Name.xml
<?xml version="1.0"?>
<config>
<modules>
<Module_name>
<active>true</active>
<codePool>local</codePool>
</Module_name>
</modules>
</config>
<modules> <Magetechno_CancelCouponcode> <version>0.1.0</version> </Magetechno_CancelCouponcode> </modules>
this must be module nameMagetechno_CancelCouponcode