我是靠谱客的博主 尊敬眼睛,最近开发中收集的这篇文章主要介绍Magento订单取消的时候还原优惠券的使用次数(Single Coupon marked as used when payment pending),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

方法不难:利用magento的观察者事件,监听“sales_order_payment_cancel”,还原优惠券的使用次数即可。

一、创建一个module

二、config.xml文件如下:


   
   
    
    
    
	    
     
     
	        
      
      
       
       0.1.0
      
      
	    
     
     
    
    
    
    
    
    
        
     
     
            
      
      
                
       
       
         Magetechno_CancelCouponcode_Model 
       
            
      
      
		
     
     
    
     
     
        
      
      
        
       
        
         
         
           singleton 
          
         
           Magetechno_CancelCouponcode_Model_Observer 
          
         
           cancel 
          
         
       
        
      
          
    
     
     
    
    
    

   
   

三、添加Observer.php文件:


   
   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');
          }
        }
      }
    }
  }
}
}



最后

以上就是尊敬眼睛为你收集整理的Magento订单取消的时候还原优惠券的使用次数(Single Coupon marked as used when payment pending)的全部内容,希望文章能够帮你解决Magento订单取消的时候还原优惠券的使用次数(Single Coupon marked as used when payment pending)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部