Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to apply Custom Discount in magento

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.75k
    Comment on it

    In magento sometimes we are requiered to apply our own discount on the cart total section. 
    To do so we can work upon the event sales_quote_collect_totals_after.

    Lets see how we can do it:

    1. Create a module with namespace as Custom and module as Discount.

    2. Then Create a file config.xml in etc folder in our module.

    3. Afterwards create a file Observer.php in Model folder.

    Now, in our config.xml file at the path Custom/Discount/etc/config.xml write the below code in it :

     

    <?xml version="1.0"?>
    <config>
     <modules>
      <Custom_Discount>
       <version>0.1.1</version>
      </Custom_Discount>
     </modules>
     <frontend>
      <events>
        <sales_quote_collect_totals_after>
          <observers>
            <discount>
              <class>custom_discount/observer</class>
              <method>setDiscount</method>
            </discount>
          </observers>
        </sales_quote_collect_totals_after> 
      </events>
       </frontend> 
    
    </config>

    In the above code we have defined our module with the version 0.1.1 and 
    then in global we have called our Observer at the event sales_quote_collect_totals_after

     

    Now in our Observer at the path Custom/Discount/Model/Observer.php write the below code:

    <?php
    class Custom_Discount_Model_Observer{
    
     
      public function setDiscount($observer) {
       
    
          $quote = $observer->getEvent()->getQuote();
          $quoteid = $quote->getId();
         
    $discountAmount = 10; //custom discount value that we want to apply
         
          if ($quoteid) {
           
              $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() - ($quote->getBaseSubtotal() * ($discountAmount/100)))
                    ->setBaseGrandTotal($quote->getBaseSubtotal() - ($quote->getBaseSubtotal() * ($discountAmount/100)))
                    ->setSubtotalWithDiscount($quote->getBaseSubtotal() - ($quote->getBaseSubtotal() * ($discountAmount/100)))
                    ->setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - ($quote->getBaseSubtotal() * ($discountAmount/100)))
                    ->save();
    
                if ($address->getAddressType() == $canAddItems) {
                  //echo $address->setDiscountAmount; exit;
                  $address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount() - ($quote->getBaseSubtotal() * ($discountAmount/100)));
                  $address->setGrandTotal((float) $address->getGrandTotal() - ($quote->getBaseSubtotal() * ($discountAmount/100)));
                  $address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount() - ($quote->getBaseSubtotal() * ($discountAmount/100)));
                  $address->setBaseGrandTotal((float) $address->getBaseGrandTotal() - ($quote->getBaseSubtotal() * ($discountAmount/100)));
                
                  if ($address->getDiscountDescription()) {
                    $address->setDiscountAmount(-($address->getDiscountAmount() - ($quote->getBaseSubtotal() * ($discountAmount/100))));
                    $address->setDiscountDescription($address->getDiscountDescription() . ','. $discountAmount. '%');
                    $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount() - ($quote->getBaseSubtotal() * ($discountAmount/100))));
                  } else {
                    $address->setDiscountAmount(-(($quote->getBaseSubtotal() * ($discountAmount/100))));
                    $address->setDiscountDescription($discountAmount. '%');
                    $address->setBaseDiscountAmount(-(($quote->getBaseSubtotal() * ($discountAmount/100))));
                  }
                 
                  $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();
              }
    
          }
       
      }
    
    }

    In the above code we are loading the product first and then with $discountAmount variable we have setup the discount percentage, which is then used to calculate the percentage value from the subtotal.
    In this way we can change any the value of $discountAmount and apply our own discount percentage.  

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: