0

In my magento store how can i go about changing the discount total in my shopping cart dynamically?

I'm able to access and get the current discount with this code:

<?php
$quote = Mage::getSingleton('checkout/cart')->getQuote();
$totals =  $quote->getTotals(); 
$discount = $totals["discount"]->getValue();
?>

I have a button in my shopping cart that when pressed should add an extra £5 onto the discount value, updating the total cost etc at the same time.

2

First of all create module control file Module name as Amit_Custommodule.xml at app/etc/modules/

  1. config.xml is app/code/community/Amit/Custommodule/etc/

code is

<?xml version="1.0" ?>
<config>
    <modules>
        <Amit_Custommodule>
            <version>1.0.0</version>
        </Amit_Custommodule>
    </modules>
    <global>
        <models>
            <custommodule>
                <class>Amit_Custommodule_Model</class>
            </custommodule>
        </models>
    </global>
    <frontend>
        <routers>
            <custommodule>
                <use>standard</use>
                <args>
                    <module>Amit_Custommodule</module>
                    <frontName>custommodule</frontName>
                </args>
            </custommodule>
        </routers>
    </frontend>
</config>

3.Amit>Custommodule>controllers>IndexController

code is

<?php

    class Amit_Custommodule_IndexController extends Mage_Core_Controller_Front_Action{

        public function applyAction(){

     $quote=Mage::getSingleton('checkout/session')->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) {
        //echo $address->setDiscountAmount; exit;
         $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
       //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();

                   }


                }

        }

     $this->_redirect('checkout/cart');

        }
        public function calcelAction(){

     $quote=Mage::getSingleton('checkout/session')->getQuote();

           $quoteid=$quote->getId();
           $discountAmount=0;
        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) {
        //echo $address->setDiscountAmount; exit;
         $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
       //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();

                   }


                }

        }

     $this->_redirect('checkout/cart');

        }
    }

For apply discount for use below url custommodule/index/apply

and cancel this discount custommodule/index/cancel

  • Can I use the above concept in removing tax also? – Milople Inc Apr 11 '15 at 6:34

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.