How to apply percentage discount to whole cart total in Magento?

How to apply percentage discount to whole cart total in Magento?

September 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to apply percentage discount to whole cart total in Magento?

Applying Percentage Discounts to Entire Cart Totals in Magento

Magento’s built-in discount rules allow for per-item discounts, but you might need to apply a percentage reduction directly to the total cart amount. This can be achieved using a combination of Magento’s core features and custom extensions or scripts.

Understanding the Current Magento Discount Calculation

By default, Magento calculates discounts in the following manner:

Standard Per-Item Discount:

Cart Item Total = Cart Item Price - (Cart Item Price * Discount Percentage)

However, you require a different calculation that affects the total cart amount after all item totals have been computed.

Implementing Custom Discount Calculation

To achieve this, you’ll need to create an observer for the sales_quote_save_before event. This event fires before a quote is saved in Magento’s database, providing an ideal point to adjust the grand total based on your custom logic.

First, let’s define the necessary observers and configuration:

app/code/Vendor/Extension/etc/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/config.xsd">
    <event name="sales_quote_save_before">
        <observer name="vendor_extension_sales_apply_custom_discount" instance="Vendor\Extension\Observer\ApplyCustomDiscount"/>
    </event>
</config>

Next, implement the observer logic:

app/code/Vendor/Extension/Observer/ApplyCustomDiscount.php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ApplyCustomDiscount implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $quote = $observer->getQuote();
        $discountPercentage = 10; // Replace this with your desired discount percentage

        if ($quote->getItemsCount() > 0) {
            $totalAmount = $quote->getSubtotal();
            $discount = $totalAmount * ($discountPercentage / 100);

            $quote->setGrandTotal($totalAmount - $discount);
        }
    }
}

Incorporating a Coupon Code

To tie this discount to a specific coupon code, you’ll need to create a custom rule that triggers the event for quotes associated with that particular coupon.

This involves creating a new rule record in Magento’s database and ensuring it is applied correctly. For more detailed information on configuring custom rules, refer to the Magento documentation on shopping cart price rules.

Conclusion

While Magento provides robust discount capabilities out-of-the-box, sometimes you need to extend its functionality to meet specific business requirements. By leveraging events and observers, you can apply custom logic that adjusts the cart total according to your defined percentage.

Remember that working with core files and events requires a solid understanding of Magento’s architecture. Always test changes in a staging environment before deploying them to production.

For more advanced discounts and promotions, consider exploring dedicated extensions available on LaravelCompany. These extensions provide pre-built functionality that can save you development time and ensure compatibility with your Magento installation.

Happy coding!

Checkout Database Extensions Configuration

Our network