Add custom discount order in total with button

Add custom discount order in total with button

August 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add custom discount order in total with button

Adding Custom Discounts to Magento Orders via Buttons

In your current setup, you’ve successfully integrated a custom discount total into the Magento sales module using the Dani_Prueba_Model_Total_Aver class. This total is applied automatically whenever an item is added to the cart. However, you now want to extend this functionality to allow for manual application and removal of the discount via dedicated buttons.

To achieve this, we’ll need to modify your existing model and create two new methods: one for applying the discount when a button is clicked, and another for removing the discount. We’ll also need to update your checkout template to include these buttons.

Modifying the Total Model

First, let’s open app/code/local/Dani/Prueba/Model/Total/Aver.php and add two new methods: applyDiscount() and removeDiscount().

public function applyDiscount(Mage_Sales_Model_Quote_Address $address) {
    $baseDiscount = 2.5;
    $discount = Mage::app()->getStore()->convertPrice($baseDiscount);

    $address->setCustomDiscount($baseDiscount);
    $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);
    $address->setGrandTotal($address->getGrandTotal() - $discount);

    return $this;
}

public function removeDiscount(Mage_Sales_Model_Quote_Address $address) {
    if ($address->hasCustomDiscount()) {
        $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getCustomDiscount());
        $address->setGrandTotal($address->getGrandTotal() + Mage::app()->getStore()->convertPrice($address->getCustomDiscount()));
    }

    return $this;
}

The applyDiscount() method behaves similarly to your existing collect() method, subtracting the custom discount from the grand total. The removeDiscount() method checks if a custom discount is already applied and, if so, adds it back to the grand totals.

Updating the Checkout Template

Next, let’s update your checkout template to include the “Apply Discount” and “Cancel Discount” buttons. Open app/design/frontend/default/your_theme/template/checkout/cart.phtml and find the <div class="cart-totals"> section.

<div class="cart-totals">
    <h2>Cart Totals</h2>

    <!-- Other totals here -->

    <div class="total">
        <label>Custom Discount: </label>
        <span id="custom-discount-value" data-base-amount="<?php echo $address->getBaseGrandTotal(); ?>"><?php echo Mage::helper('core')->currency($address->getGrandTotal(), true, false); ?></span>
    </div>

    <?php if ($address->hasCustomDiscount()): ?>
        <button type="button" id="cancel-discount-btn">Cancel Discount</button>
    <?php else: ?>
        <button type="button" id="apply-discount-btn">Apply Discount</button>
    <?php endif; ?>

    <!-- Other totals here -->

    <div class="a-right">
        <strong>Grand Total:</strong>
        <span data-base-amount="<?php echo $address->getBaseGrandTotal(); ?>"><?php echo Mage::helper('core')->currency($address->getGrandTotal(), true, false); ?></span>
    </div>

</div>

In this updated code, we’ve added a new div for the “Custom Discount” total and the buttons for applying or canceling the discount. We’re also using JavaScript to store the original grand total value in data attributes.

Adding JavaScript Functionality

Finally, let’s add some JavaScript functionality to handle button clicks. Open js/your_theme.js (or create it if it doesn’t exist) and add the following code:

require(['jquery'], function($) {
    $(document).ready(function() {

        $('#apply-discount-btn').click(function() {
            $.ajax({
                url: '<?php echo Mage::getUrl("prueba/index/applyDiscount"); ?>',
                success: function(response) {
                    $("#custom-discount-value").html(response.discount);
                    $("#cancel-discount-btn").show();
                    $(this).hide();
                }
            });
        });

        $('#cancel-discount-btn').click(function() {
            $.ajax({
                url: '<?php echo Mage::getUrl("prueba/index/removeDiscount"); ?>',
                success: function(response) {
                    $("#custom-discount-value").html(response.originalTotal);
                    $("#apply-discount-btn").show();
                    $(this).hide();
                }
            });
        });

    });
});

This JavaScript code handles the click events for both buttons. When the “Apply Discount” button is clicked, it sends an AJAX request to a new controller action (applyDiscount) and updates the discount value in the template. When the “Cancel Discount” button is clicked, it sends an AJAX request to the removeDiscount action and restores the original total.

Conclusion

In this post, we’ve extended your custom discount functionality to allow for manual application and removal of the discount via dedicated buttons in the checkout process. By modifying your existing model and adding JavaScript functionality, you can now provide a more flexible shopping experience for your customers.

Remember to create the required controller actions (applyDiscount and removeDiscount) in your module’s controllers/IndexController.php file to handle the AJAX requests from the JavaScript code.

Don’t forget to check out Laravel Company for more detailed tutorials and guides on Magento development.

Checkout Extensions Themes Orders

Our network