Applying a 10% Discount on Grand Total Price in Magento
In this post, we’ll guide you through applying a 10% discount to the grand total price of orders for specific users in Magento 1.9.0.1. This discount will be visible on the cart page, order review page, and in the admin panel.
Understanding the Magento Discount Structure
Before diving into the code, let’s quickly recap how discounts work in Magento:
- Cart Discounts: These are applied to individual line items (products) within the cart.
- Cart Total Discounts: These are applied to the total price of all items in the cart.
For your requirement, we’ll create a Cart Total Discount rule that applies a percentage discount to the grand total. This approach gives us more control over when and how the discount is applied.
Creating the Custom Discount Module
First, let’s create a custom module to house our discount logic. Follow these steps:
-
Create a new directory under
app/code/localwith the following structure:app/code/local/Namespace/Module/etc/config.xml app/code/local/Namespace/Module/Model/ Observer.php -
In
config.xml, define your module configuration:<?xml version="1.0"?><config> <modules> <Namespace_Module> <active>true</active> <codePool>local</codePool> </Namespace_Module> </modules> </config> -
Create the Observer class
Observer.phpthat will handle cart total discounts:namespace Namespace\Module\Model; class Observer { public function applyDiscount(Varien_Event_Observer $observer) { // Your discount logic goes here } }
Implementing the Discount Logic
Next, we’ll add the actual discount calculation inside the applyDiscount method.
-
In
app/etc/modules/Namespace_Module.xml, enable the event observer:<?xml version="1.0"?> <config> <modules> <Namespace_Module> <active>true</active> <codePool>local</codePool> <depends> <Mage_CartTotalRule /> </depends> </Namespace_Module> </modules> </config> -
Update the
Observer.phpfile with your discount rules:namespace Namespace\Module\Model; class Observer { public function applyDiscount(Varien_Event_Observer $observer) { // Get the quote object $quote = $observer->getQuote(); // Check if this is a specific user or order condition here if (/* Your custom condition */) { // Calculate 10% of the grand total $discountAmount = round($quote->getGrandTotal() * 0.1, 2); // Set the discount amount and type $quote->setDiscountDescription('10% Grand Total Discount'); $quote->setDiscountAmount($discountAmount); $quote->setDiscountTaxCalculation(1); } } }
Applying the Discount to Specific Users
To apply this discount only to specific users, you can modify the condition inside the if statement.
For example, if you want to give a 10% discount for customers with an ID between 50 and 100:
// Check if this is a specific user or order condition here
if ($quote->getCustomerId() >= 50 && $quote->getCustomerId() <= 100) {
// Rest of the code...
}
Conclusion
In this post, we demonstrated how to create a custom module and observer to apply a 10% discount on the grand total price in Magento. By creating a Cart Total Discount rule, you have more control over when and to whom the discount is applied.
Remember to replace Namespace with your actual namespace and ensure that the condition inside the if statement meets your specific business requirements.
For more information on customizing Magento’s cart and pricing logic, consider checking out our comprehensive guide on modifying Magento prices.
Happy coding!