Search code examples
magentomagento-1.8discountcoupon

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


I want to apply a 10 percent discount for the whole total. However, if the discount amount is greater than $100, I would like to only apply $100 fixed price instead of 10%. For example if 2 items total to $200, the 10% will apply and if the total is $2,000, only apply $100 discount.

I am using an observer

sales_quote_collect_totals_after

and that seems to work but don't know the code logic.

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

        }

Every time I run this code, the item changes its price and the whole total as well. I don't want the items to change their price, only the grand total and the discount amount. Does any one know how I would go about doing this.

Thanks.


Solution

  • May this can help you check that out.as keep the code that much you needed.

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