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
    • 1.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"?>

    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:

    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();
              $canAddItems = $quote->isVirtual() ? ('billing') : ('shipping');
              foreach ($quote->getAllAddresses() as $address) {
                $quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
                $quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());
                    (float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
                    (float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
                $quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
                $quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());
                $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)))
                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))));
                }//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

Sign up using

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: