Magento: Applying Discounts to Shipping Amount
Discounting shipping costs is a powerful way to incentivize customers and boost sales, but it can be challenging to implement correctly in Magento. In this post, we’ll walk through the process of applying discounts specifically to the shipping amount, rather than the total basket price.
Understanding the Shopping Cart Price Rule
Magento’s built-in Shopping Cart Price Rule allows you to apply discounts based on various conditions. When you enable “Apply to Shipping Amount,” the discount should theoretically be subtracted from the shipping cost alone. However, many users report that the discount applies to the entire order amount instead of just the shipping.
This discrepancy often occurs because Magento calculates the discount incorrectly during the checkout process. To resolve this issue, we need to manipulate the rule settings and override some core files.
Step 1: Create a New Shopping Cart Price Rule
Begin by creating a new price rule:
- Go to Marketing > Promotions > Shopping Cart Price Rules.
- Click “Add New Rule.”
- Give your rule a descriptive name (e.g., “Free Shipping for Second Item”).
- Set the conditions based on your requirements. For example, set “If ALL of these conditions are TRUE” and then specify that the item quantity must be at least 2.
Step 2: Configure the Discount
Under the Actions section:
- Set “Apply to Shipping Amount” to Yes.
- Enter the discount value you want to apply to the shipping cost.
Step 3: Override Magento Core Files
To ensure the discount applies only to the shipping amount, we need to override two core files:
app/code/core/Mage/Sales/Model/Quote/Address/Total.phpapp/code/core/Mage/Sales/Model/Quote/Item/Abstract.php
Create local overrides for these files:
app/code/local/Mage/Sales/Model/Quote/Address/Total.php
app/code/local/Mage/Sales/Model/Quote/Item/Abstract.php
In the Total.php file, find the _getAppliedDiscounts() function and modify it to check if the rule is applied to the shipping amount:
$appliedRules = Mage::getSingleton('sales/quote_address_total_price_rule')->getAppliedDiscounts($this);
foreach ($appliedRules as $ruleId => $discount) {
// Check if the discount applies to the shipping amount
$discountRule = Mage::getModel('salesrule/rule')->load($ruleId);
if ($discountRule->getApplyToShipping() === 1 && $discountRule->getDiscountStep() == 'fixed') {
continue; // Skip rules that apply to shipping and are fixed discounts
}
}
In the Abstract.php file, find the _mergeTaxes() function and update it to exclude discounts applied to the shipping amount:
$itemDiscount = 0;
foreach ($this->getAppliedDiscounts() as $ruleId => $discount) {
// Check if the discount applies to the shipping amount
$discountRule = Mage::getModel('salesrule/rule')->load($ruleId);
if ($discountRule->getApplyToShipping() === 1 && $discountRule->getDiscountStep() == 'fixed') {
continue; // Skip rules that apply to shipping and are fixed discounts
}
$itemDiscount += $discount;
}
Step 4: Clear Cache and Reindex
After making these modifications, clear your Magento cache and reindex all data:
- Go to System > Cache Management.
- Click “Flush Magento Cache.”
- Navigate to System > Index Management.
- Select all indexes and click “Reindex All.”
Conclusion
Applying discounts specifically to the shipping amount in Magento requires a combination of rule configuration and core file overrides. By following this guide, you can ensure that your discounts are applied correctly during the checkout process.
Remember to always create local overrides when modifying core files to preserve upgrade compatibility. The code examples provided above demonstrate how to modify the necessary functions, but there may be other scenarios specific to your store’s configuration.
For more advanced promotions and pricing strategies in Magento, consider exploring our comprehensive guides on discount management, price rules, and promotional campaigns at Laravel Company.
Happy coding!