dongxu1029 2014-02-19 11:19
浏览 47
已采纳

Magento - 如何在观察者中不使用优惠券代码添加自定义折扣

How can I set discount (not using coupon code) in observer that should be applied on cart, order review(checkout/onepage), Invoice & Memo, this solution only worth of applying discount to cart http://www.magentocommerce.com/boards/viewthread/269154/

And solution like http://magento.ikantam.com/qa/how-add-discount-total-magento cannot work for applying discount from observer

Update :

Adding Answer from link here

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>1.0.10</version>
        </Namespace_Modulename>
    </modules>
       <global>
      <events>
            <sales_quote_collect_totals_after>
              <observers>
                  <namespace>
                    <type>singleton</type>
                    <class>Namespace_Modulename_Model_Observer</class>
                    <method>setDiscount</method>
                  </namespace>
              </observers>
           </sales_quote_collect_totals_after>             
      </events>
  </global>

Observer.php

class Namespace_Modulename_Model_Observer
{
   public function setDiscount($observer)
   {
     $quote         =  $observer->getEvent()->getQuote();
     $quoteid       =  $quote->getId();
     $discountAmount=  10;

     if($quoteid) {

       if($discountAmount>0) {

         $total=$quote->getBaseSubtotal();
         $quote->setSubtotal(0);
         $quote->setBaseSubtotal(0);
         $quote->setSubtotalWithDiscount(0);
         $quote->setBaseSubtotalWithDiscount(0);
         $quote->setGrandTotal(0);
         $quote->setBaseGrandTotal(0);
         $canAddItems = $quote->isVirtual()? ('billing') : ('shipping'); 

         foreach ($quote->getAllAddresses() as $address) {

           $address->setSubtotal(0);
           $address->setBaseSubtotal(0);
           $address->setGrandTotal(0);
           $address->setBaseGrandTotal(0);
           address->collectTotals();
           $quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
           $quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());
           $quote->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount());
           $quote->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount());
           $quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
           $quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
           $quote ->save();
           $quote->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
                 ->setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
                 ->setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
                 ->setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
                 ->save(); 


                if($address->getAddressType()==$canAddItems) {
                   $address->setSubtotalWithDiscount((float)$address->getSubtotalWithDiscount()-$discountAmount);
                   $address->setGrandTotal((float) $address->getGrandTotal()-$discountAmount);
                   $address->setBaseSubtotalWithDiscount((float)$address->getBaseSubtotalWithDiscount()-$discountAmount);
                   $address->setBaseGrandTotal((float)$address->getBaseGrandTotal()-$discountAmount);
                if($address->getDiscountDescription()){
                $address->setDiscountAmount(-($address->getDiscountAmount()-$discountAmount));
                     $address->setDiscountDescription($address->getDiscountDescription().', Custom Discount');
                $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
                }else {
                    $address->setDiscountAmount(-($discountAmount));
                    $address->setDiscountDescription('Custom Discount');
                    $address->setBaseDiscountAmount(-($discountAmount));
                }
                $address->save();
           }//end: if
        } //end: foreach

        foreach($quote->getAllItems() as $item){
             $rat=$item->getPriceInclTax()/$total;
             $ratdisc=$discountAmount*$rat;
             $item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
             $item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
        }
        }
    }
 }
}
  • 写回答

1条回答 默认 最新

  • dsgrgaz321973284 2014-02-19 12:30
    关注

    Hi may be worth try with this url http://pradeepkumarrcs.blogspot.in/2011/12/applying-custom-discount-amount-in.html

    I've used this code explain in blog, it uses sales_quote_collect_totals_after so it would fire every time total is calculated and you custom discount is applied on cart, Discount will be display in order and invoice and also in credit memo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳