Display Catalog Price Rule Discount on Magento Shopping Cart
As an experienced e-commerce developer, I frequently encounter this common challenge when working with Magento: how to accurately display catalog price rule discounts across the entire shopping experience, including the cart page.
Understanding the Default Behavior
By default in Magento, when a catalog price rule is applied (let’s say for a 20% discount on all products), it correctly updates the special price and applies the discount. However, you’ll notice that on the product list and view pages, both the old price and the discounted special price are displayed:
Old Price: £10
Special Price: £8 (20% off)
But when you add these items to the cart, only the special (discounted) price is shown. The original price or discount percentage isn’t immediately visible.
Why You Need to Display the Discount on the Cart
Displaying the full details of the discount on the shopping cart page provides several benefits:
- Transparency: It clearly communicates to customers what they are actually saving.
- Reassurance: It validates that their discount was applied correctly.
- Comparison: It allows customers to see the original price for reference.
Displaying Discounts in Magento
To achieve this, you’ll need to modify the shopping cart blocks and templates. Here’s a step-by-step guide:
Step 1: Create a Custom Discount Extension
First, create a custom extension that calculates the discount amount and percentage. You can follow our guide on Creating Custom Extensions for detailed instructions.
In your new module’s etc/config.xml file, define a configuration node:
<config>
<default>
<custom_discount_active>1</custom_discount_active>
<custom_discount_message>Discount Applied: %s%%</custom_discount_message>
</default>
</config>
This enables the discount feature and sets the default message format.
Step 2: Update the Cart Model
In your custom module’s app/code/Local/CustomDiscount/Model/Observer.php, create an observer that calculates the discounts:
class Observer implements ObserverInterface
{
public function applyCatalogPriceRule(Varien_Event_Observer $observer)
{
// Calculate discount here using Magento's catalog price rule API
$items = $observer->getCart()->getQuote()->getAllVisibleItems();
foreach ($items as $item) {
$ruleCollection = Mage::getResourceModel('salesrule/rule_collection')
->addFieldToFilter('is_active', 1)
->addFieldToFilter('product_id', $item->getProductId())
->addFieldToFilter('from_date', array('date' => true, 'to_date' => true))
->setOrder('priority', Varien_Data_Collection::SORT_ORDER_DESC);
// Apply your discount calculation logic here
}
}
}
Step 3: Update the Cart Block
Now, update your cart block to display the calculated discounts. In app/code/Local/CustomDiscount/Block/Cart/Item.php, add a new method:
public function getDiscountedPrice()
{
$item = $this->getQuoteItem();
// Implement your logic here using the data from Step 2
return $discountedPrice;
}
public function getDiscountPercent()
{
// Calculate and return the discount percentage
}
Step 4: Update the Cart Template
Finally, update the cart template to display the calculated discounted price and percent. In app/design/frontend/default/yourtheme/template/checkout/cart/item/default.phtml, add your custom blocks:
<div class="cart-item-price">
<span class="price">Old Price: £<?php echo $item->getProduct()->getPrice() ?></span>
<span class="price">Discounted Price: £<?php echo $this->helper('customdiscount')->getDiscountedPrice($item) ?></span>
<span class="discount-percent">(<?php echo $this->helper('customdiscount')->getDiscountPercent($item) ?>% off)</span>
</div>
Conclusion
Displaying catalog price rule discounts clearly on the Magento shopping cart page requires a combination of custom extensions, observers, and template modifications. By following this guide, you can enhance your customers’ experience by providing full transparency about their savings.
Remember that Magento’s flexibility allows for a wide range of customizations. Always test your changes thoroughly in multiple browsers and devices to ensure consistency across the board.
For more advanced pricing strategies or complex discounts, consider exploring our post on Magento price rules for deeper insights.
Happy developing!