根据Magento的总金额,在总计中应用自定义折扣

时间:2015-04-13 18:27:28

标签: magento magento-1.8 discount coupon

我想对整个总额申请10%的折扣。但是,如果折扣金额大于100美元,我只想申请100美元固定价格而不是10%。例如,如果2件物品总计200美元,则10%将适用,如果总额为2,000美元,则仅适用100美元折扣。

我正在使用观察员

sales_quote_collect_totals_after

这似乎有效但不了解代码逻辑。

        $discountAmount= ((float) $oCoupon->getDiscountAmount()/100) *$total;
        if ($discountAmount>100) {
            foreach($quote->getAllItems() as $item){

                $item->setDiscountAmount(100);
                $item->setBaseDiscountAmount(100);
                $item->setCustomPrice($total - 100);

                $item->getProduct()->setIsSuperMode(true);
                $item->save();
            }

        }

每次运行此代码时,该项目都会更改其价格和整个代码。我不希望这些物品改变价格,只需要总金额和折扣金额。有没有人知道我会这样做。

感谢。

1 个答案:

答案 0 :(得分:0)

这可以帮助你检查出来。保留你需要的代码。

 $quote=$observer->getEvent()->getQuote();
       $quoteid=$quote->getId();
                  $total=$quote->getBaseSubtotal();
    //check condition here if need to apply Discount      $discountAmount= ((float) $oCoupon->getDiscountAmount()/100) *$total;



 if($quoteid) {
           if($discountAmount>100) {
       $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().', Amount Waived');
         $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
         }else {
         $address->setDiscountAmount(-($discountAmount));
         $address->setDiscountDescription('Amount Waived');
         $address->setBaseDiscountAmount(-($discountAmount));
         }
         $address->save();
        }//end: if
       } //end: foreach
       //echo $quote->getGrandTotal();

      foreach($quote->getAllItems() as $item){
                     //We apply discount amount based on the ratio between the GrandTotal and the RowTotal
                     $rat=$item->getPriceInclTax()/$total;
                     $ratdisc=$discountAmount*$rat;
                     $item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
                     $item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();

                   }


                }

        }
     }