Displaying Multiple Discounts in Magento 1.7 Cart
As an e-commerce developer, I often encounter requests from clients who want to provide their customers with a clearer breakdown of discounts applied during checkout. In your case, you’re looking to separate the free shipping and coupon code discounts for better transparency.
The default Magento behavior combines all discounts into a single line, which can make it difficult for customers to understand what they’re actually saving on each item or action. To achieve the desired layout without relying on paid extensions, we’ll need to modify some core files.
Step 1: Understanding the Discount Structure
In your current setup, Magento applies multiple discounts by running them sequentially. The first discount is applied to the subtotal, then the next one is run against the remaining total after the first discount has been subtracted. This cumulative approach leads to the combined display you’re seeing.
Step 2: Modifying the Discount Display
To separate these discounts, we’ll need to alter how Magento displays them in the cart. Here’s a step-by-step guide:
-
Navigate to
app/code/core/Mage/Sales/Block/Quote/Totals.php. -
Look for the function
public function getSummary()(around line 80). -
Within this function, find the section that generates the discounts:
$discount = new Varien_Object();
if ($total->getDiscountDescription()) {
$discount->setCode($total->getDiscountDescription());
} else {
$discount->setCode(Mage::helper('sales')->__('Discount'));
}
Replace this with the following code to separate discounts:
$discounts = array();
foreach ($total->getAllItems() as $item) {
if ($item['code'] !== null && $item['value'] !== 0) {
$discounts[] = Mage::helper('sales')->__($item['label']) . ': ' . Mage::app()->getLocale()
->currency(Mage::app()->getStore()->getCurrentCurrencyCode())
->toCurrency(abs(floatval($item['value'])));
}
}
if (!empty($discounts)) {
$discount = implode('<br />', $discounts);
} else {
$discount = Mage::helper('sales')->__('Discount');
}
This modified code iterates through all discount items and builds an array of separate discounts. It then uses implode to join them into a single string, with each discount on its own line.
- Save the changes and clear your cache using
php magento-cache-clean.php.
Step 3: Verifying the Changes
After making these modifications, refresh the checkout page or load a cart that qualifies for both discounts. You should now see each discount (free shipping and coupon code) listed separately:
Subtotal: $xxx.xx
Discount(Free Shipping): -$xx.xx
Discount(Coupon Code RUSH13): -$xx.xx
Conclusion
By modifying the core Totals.php file, we’ve successfully separated the multiple discounts in your Magento 1.7 cart without the need for additional extensions.
Remember that modifying core files should be done with caution, as it can potentially lead to compatibility issues during updates or upgrades. Always backup your files before making changes and test thoroughly on a staging environment before deploying to production.
If you’re looking to extend this functionality further or streamline other aspects of your e-commerce platform, consider exploring the resources available at Laravel Company. They offer comprehensive guides, tutorials, and services tailored for Magento developers.