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

Magento 根据Mangento中的总计金额,对总计应用自定义折扣,magento,magento-1.8,discount,coupon,Magento,Magento 1.8,Discount,Coupon,我想申请全部10%的折扣。但是,如果折扣金额大于100美元,我只想采用100美元的固定价格,而不是10%。例如,如果两个项目的总额为200美元,则将适用10%的折扣;如果总额为2000美元,则仅适用100美元的折扣 我用的是观察者 sales_quote_collect_totals_after 这似乎有效,但不知道代码逻辑 $discountAmount= ((float) $oCoupon->getDiscountAmount()/100) *$total;

我想申请全部10%的折扣。但是,如果折扣金额大于100美元,我只想采用100美元的固定价格,而不是10%。例如,如果两个项目的总额为200美元,则将适用10%的折扣;如果总额为2000美元,则仅适用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();
            }

        }
每次我运行这段代码时,这个项目都会改变它的价格和总价格。我不希望这些商品改变它们的价格,只希望总数和折扣金额。有人知道我会怎么做吗


谢谢。

希望这能帮你检查一下。请保留你所需要的代码

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

                   }


                }

        }
     }

非常感谢您的回复。不幸的是,我使用了这个代码,它并不完全是我所需要的。我忘了提到有一个10%的购物车价格规则,当应用优惠券时,无论我做什么,它总是应用10%,我不能将其更改为100美元。我想说的是,优惠券不仅仅用于一个客户,因此这条规则的适用范围将有所不同。只需稍作修改,我就能够让它发挥作用。我现在正试图找出如何为整个订单提供100美元的折扣,但它所做的是为每件商品增加100美元,如果我的购物车中有两件商品的总价值超过2000美元,我可以从100美元中获得200美元的折扣。你知道怎么做吗?我发现这很有用,谢谢,但我需要一种方法在条件为真时取消折扣,所以我不知道如何使用没有折扣的全额重新计算。