1

I have added a currency switcher as per the inchoo tutorial

The issue is though when a customer changes the currency, the call is a physical page load and not an Ajax call which in turn means if they use the browser's back button they need to click it twice to actually go back to the last page.

Is there a way to change this or what's the best way to change the currency via Ajax?

2

The problem is the build in currency switcher, redirects back to the homepage, so you can't use it for an Ajax call. But it's not to hard to create the same switch URL without a redirect.

To make it possible to switch the currency by an ajax call you need to recreate the Mage_Directory_CurrencyCpntroller, only without the redirect. You can do so by creating your module registration in app/etc/modules/Hans_SwitchCurrency.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Hans_SwitchCurrency>
            <active>true</active>
            <codePool>community</codePool>
        </Hans_SwitchCurrency>
    </modules>
</config>

Then create your module config app/code/community/Hans/SwitchCurrency/etc/config.xml:

<?xml version="1.0"?>
<config>
    <frontend>
        <routers>
            <switchcurrency>
                <use>standard</use>
                <args>
                    <module>Hans_SwitchCurrency</module>
                    <frontName>switchcurrency</frontName>
                </args>
            </switchcurrency>
        </routers>
    </frontend>
</config>

And the controller in app/code/community/Hans/SwitchCurrency/controllers/SwitchController.php:

class Hans_SwitchCurrency_SwitchController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        if ($curency = (string) $this->getRequest()->getParam('currency')) {
            Mage::app()->getStore()->setCurrentCurrencyCode($curency);
        }
    }
}

Then you can create your Ajax call to <?php echo $this->getUrl('currencyswitch/switch', array('currency' => $_code)); ?>, to switch the currency without redirecting back to the homepage. Instead you could create a page refresh using javascript or something.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.