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

时间:2014-02-19 11:19:06

标签: php magento magento-1.7

如何在观察者中设置应该在购物车上使用的折扣(不使用优惠券代码),订单评论(结帐/一页),发票和&备忘录,这个解决方案只适用于购物车折扣 http://www.magentocommerce.com/boards/viewthread/269154/

http://magento.ikantam.com/qa/how-add-discount-total-magento这样的解决方案无法用于从观察者

申请折扣

更新:

从链接添加答案

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 个答案:

答案 0 :(得分:2)

您可能值得尝试使用此网址http://pradeepkumarrcs.blogspot.in/2011/12/applying-custom-discount-amount-in.html

我在博客中使用了此代码说明,它使用sales_quote_collect_totals_after因此每次计算总数时都会触发,并且您在购物车上应用自定义折扣,折扣将按订单和发票以及信用额显示备忘录。