西部数码主机 | 阿里云主机| 虚拟主机 | 服务器 | 返回乐道官网
当前位置: 主页 > php教程 > magento教程 >

Magento订单取消还原优惠券的使用次数

时间:2015-08-09 08:51来源:未知 作者:好模板 点击:
方法不难:利用magento的观察者事件,监听sales_order_payment_cancel,还原优惠券的使用次数即可。 一、创建一个module 二、config.xml文件如下: config modules Magetechno_CancelCouponcode version0.1.0/ver
方法不难:利用magento的观察者事件,监听“sales_order_payment_cancel”,还原优惠券的使用次数即可。
一、创建一个module
 
二、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>
 
cancel_coupon_config.xml
三、添加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(),null,"coupon_reused.log",true);
  Mage::log("Coupon used time above",null,"coupon_reused.log",true);
 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",null,"coupon_reused.log",true);
$__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",null,"coupon_reused.log",true);
 
}
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",null,"coupon_reused.log",true);
}
          }
          if ($customerCoupon->getTimesUsed() > 0) {
            $customerCoupon->setTimesUsed($customerCoupon->getTimesUsed()-1);
            $customerCoupon->save();
//Mage::log('setTimesUsed customer saved Rule Time used');
          }
        }
      }
    }
  }
}
}
(责任编辑:好模板)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容