4

How can I set discount (not using coupon code) in observer that should be applied on cart, order review(checkout/onepage), Invoice & Memo, this solution only worth of applying discount to cart http://www.magentocommerce.com/boards/viewthread/269154/

And solution like http://magento.ikantam.com/qa/how-add-discount-total-magento cannot work for applying discount from observer

Update :

Adding Answer from link here

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Modulename>
            <version>1.0.10</version>
        </Namespace_Modulename>
    </modules>
       <global>
      <events>
            <sales_quote_collect_totals_after>
              <observers>
                  <namespace>
                    <type>singleton</type>
                    <class>Namespace_Modulename_Model_Observer</class>
                    <method>setDiscount</method>
                  </namespace>
              </observers>
           </sales_quote_collect_totals_after>             
      </events>
  </global>

Observer.php

class Namespace_Modulename_Model_Observer
{
   public function setDiscount($observer)
   {
     $quote         =  $observer->getEvent()->getQuote();
     $quoteid       =  $quote->getId();
     $discountAmount=  10;

     if($quoteid) {

       if($discountAmount>0) {

         $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().', Custom Discount');
                $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
                }else {
                    $address->setDiscountAmount(-($discountAmount));
                    $address->setDiscountDescription('Custom Discount');
                    $address->setBaseDiscountAmount(-($discountAmount));
                }
                $address->save();
           }//end: if
        } //end: foreach

        foreach($quote->getAllItems() as $item){
             $rat=$item->getPriceInclTax()/$total;
             $ratdisc=$discountAmount*$rat;
             $item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
             $item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();
        }
        }
    }
 }
}
  • so you want to add a discount but without it showing up in the totals like a discount rule does? – Sander Mangel Feb 19 '14 at 11:35
  • @SanderMangel Discount Description should be shown, I am just not using rules, so thats why I cant apply discount using coupon code – Ahmed Feb 19 '14 at 11:40
  • @Ahmed But this makes store-credit mis-functioning.. – Anurag Khandelwal Dec 8 '17 at 14:29
8

Hi may be worth try with this url http://pradeepkumarrcs.blogspot.in/2011/12/applying-custom-discount-amount-in.html

I've used this code explain in blog, it uses sales_quote_collect_totals_after so it would execute every time total is calculated and your custom discount is applied on cart, Discount will be display in order,invoice and also in credit memo.

  • Bookmarked the link! So it applies a discount without having to create a coupon before hand? Nice! – Sander Mangel Feb 19 '14 at 11:49
  • Not sure about setting totals mannualy. Doesn't look like a good practice. What will happen with current applied cart rules? – Ricardo Martins Aug 26 '14 at 18:07
  • 1
    @RicardoMartins current cart rule will also apply. – Mufaddal Aug 27 '14 at 4:24
  • Have you checked this with other currencies? This wont work – Magento Learner Jul 23 '15 at 13:21
  • Tax calculation is not working properly. it always consider "Apply Customer Tax" as "Before discount" – Minesh Patel Jun 26 '17 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.