0

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> 
1
  • it is mention in your question <modules> <Magetechno_CancelCouponcode> <version>0.1.0</version> </Magetechno_CancelCouponcode> </modules> this must be module name Magetechno_CancelCouponcode Commented Jul 15, 2016 at 10:19

2 Answers 2

1

Replace Module_name with Magetechno_CancelCouponcode

app/etc/modules/Magetechno_CancelCouponcode.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Magetechno_CancelCouponcode>
            <active>true</active>
            <codePool>local</codePool>
        </Magetechno_CancelCouponcode>
    </modules>
</config> 
0
2

Here module_name is your custom module name,

Its Module entry point,

Magetechno_CancelCouponcode.xml

so you have to keep Package_Modulename with your Package and module name.

Remainng all files are same.

If this file is not defined your module is not works or not display inside site.

Change Module_name with Magetechno_CancelCouponcode

<?xml version="1.0"?>
<config>
    <modules>
        <Module_name>
            <active>true</active>
            <codePool>local</codePool>
        </Module_name>
    </modules>
</config> 
1
  • Thanks a lot for support. very nice explaination. as other user answered first, i accepted that..... Commented Jul 15, 2016 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.