【问题标题】:Apply custom discount to the grand total depending on the grand total amount in Mangento根据 Magento 中的总金额对总金额应用自定义折扣
【发布时间】:2015-04-13 18:27:27
【问题描述】:

我想为整个总额应用 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();
            }

        }

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

谢谢。

【问题讨论】:

    标签: magento magento-1.8 discount coupon


    【解决方案1】:

    这可以帮助您检查一下。保留您所需要的代码。

     $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 美元,如果我的购物车中有 2 件商品的总价值超过 2000 美元,我将获得 200 美元的折扣而不是 100 美元.知道该怎么做吗?
    • 我发现这很有用,谢谢,但我需要一种在条件为真时取消折扣的方法,所以我不知道如何使用没有折扣的全价再次重新计算。
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多