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


<?xml version="1.0"?>


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

     if($quoteid) {

       if($discountAmount>0) {

         $canAddItems = $quote->isVirtual()? ('billing') : ('shipping'); 

         foreach ($quote->getAllAddresses() as $address) {

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

                if($address->getAddressType()==$canAddItems) {
                   $address->setGrandTotal((float) $address->getGrandTotal()-$discountAmount);
                     $address->setDiscountDescription($address->getDiscountDescription().', Custom Discount');
                }else {
                    $address->setDiscountDescription('Custom Discount');
           }//end: if
        } //end: foreach

        foreach($quote->getAllItems() as $item){
             $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

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.

| improve this answer | |
  • 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

If you are looking for an extension then this might help https://redchamps.com/instan-shopping-cart-magento-2-extension.html this will allow you to apply a custom amount directly to customer's shopping cart or to the order being created from admin.

Please note that this is a paid extension and I am the developer of it.

| improve this answer | |

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.