Changing Base Currency Programmatically in Magento CE
Hello developers! Today we’re going to tackle an interesting challenge faced by one of our readers who’s using Magento Community Edition (CE) version 1.9.1.0.
The Challenge: Maintaining INR as Base Currency while Enabling PayPal for International Customers
Our reader has a single Magento website with the base currency set to Indian Rupees (INR). They have two storesâone for domestic customers and one for international customers, which they switch between in the index.php file based on customer country determined by their IP address.
The catch is that PayPal payment gateway only works for international customers but requires a base currency other than INR to display as an option during checkout. If the base currency is set to INR, the PayPal method disappears from the payment methods list.
Our Approach: Programmatically Changing Base Currency on Checkout Page
To resolve this issue without changing the base currency in the Magento admin panel, we can modify the code to programmatically change the base currency for international customers when they reach the checkout page. This way, INR remains as the default base currency for most of your website, but USD or another supported PayPal currency is used during the checkout process for international customers.
Step-by-Step Solution
- Create a Copy of Your Checkout Page
Make sure you have a backup of your current checkout page file (checkout/onepage.phtml). Then, create a copy of this file and name it something like onepage_international.php to keep things organized.
- Check Customer Country in the New File
In the newly created file (onepage_international.php), add PHP code at the top to check if the current customer is from India or not using their IP address or session data where you’re already storing this information based on your country detection logic in index.php.
- Conditionally Include the Base Currency File
Based on whether the customer is international (not from India) or domestic (from India), conditionally include either checkout/onepage.phtml or a new version of onepage_international.php that has the base currency set to USD or another supported PayPal currency.
Here’s an example of how your conditional code might look:
<?php
// Check if customer is international using your existing logic
$isInternational = /* Your logic here, e.g. checking session data or IP address */;
if ($isInternational) {
// Include the modified checkout page for international customers with base currency set to USD
include 'path/to/your/custom/onepage_international_usa.php';
} else {
// Include the original checkout page for domestic customers with base currency set to INR
include 'checkout/onepage.phtml';
}
?>
- Modify the Base Currency in the New Checkout Page
In your new onepage_international_usa.php file, you’ll need to modify the base currency by updating the following line:
// Change this line from the original checkout page
$this->getLayout()->createBlock('checkout/onepage_failure')->setTemplate('checkout/onepage/failure.phtml');
// To something like this for international customers with USD as base currency
$this->getLayout()->createBlock('checkout/onepage_failure')
->setTemplate('checkout/onepage/failure_usa.phtml');
Then, in the failure_usa.phtml file that you create or modify (you’ll find it under app/design/frontend/default/package/template/checkout/onepage), make sure to include the USD base currency using Magento’s currency code.
Conclusion
By following this approach, you can maintain INR as your primary base currency while still offering PayPal as a payment option for international customers. This solution allows you to keep your product pricing in INR from your ERP and only switch the base currency temporarily during the checkout process when needed.
Remember to thoroughly test this change across different scenarios and browsers to ensure it works consistently for your users. If you need further assistance or have any questions about implementing this solution, feel free to reach out to our expert Magento developers at Laravel Company. We’re always here to help!