根据 Magento 中的总金额对总金额应用自定义折扣

Apply custom discount to the grand total depending on the grand total amount in Mangento

我想对总价申请 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();
            }

        }

每次我 运行 此代码时,项目都会更改其价格和总计。我不希望商品改变价格,只希望改变总计和折扣金额。有谁知道我会怎么做。

谢谢。

希望这可以帮助您检查 out.as 保留您需要的代码。

 $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();

                   }


                }

        }
     }