Magento – How to add currency suffix to URL links

magento-1.7magento-enterprise

I need to add a currency suffix to url links for the products.
For example: When switching currency to CAD or USD in ANY Magento store. There is NO code in the URL links which states the currency.

www.example.com/pencils.html

I need is this: When switching currency to USD
link should look like:

www.example.com/pencils.html?currency=usd

When switching currency to CAD
link should look like:

www.example.com/pencils.html?currency=cad

Best Answer

I have found the solution.... which default magento currency change url Copy app>code>Core>Mage>Directory>Helper>Url.php

to app>local>Core>Mage>Directory>Helper>Url.php

and change the code of function

public function getSwitchCurrencyUrl($params = array())
{
    $params = is_array($params) ? $params : array();


    if ($this->_getRequest()->getAlias('rewrite_request_path')) {
        $url = Mage::app()->getStore()->getBaseUrl() . $this->_getRequest()->getAlias('rewrite_request_path');
    }
    else {
        $url = $this->getCurrentUrl();
    }
     $params[Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED] = Mage::helper('core')->urlEncode($url);
    if(strpos($_SERVER["REQUEST_URI"],'?')){
    $CurrentUrl=$_SERVER["REQUEST_URI"].'&_currency='   .$params['currency'];
    }else{
        $CurrentUrl=$_SERVER["REQUEST_URI"].'?_currency='   .$params['currency'];
    }
    return $CurrentUrl;
    //return $this->_getUrl('directory/currency/switch', $params);

}

Then goto copy app/code/core/Mage/Core/Model/Url.php

to app/code/local/Mage/Core/Model/Url.php

function setRouteParams add below code

        if(!is_null($this->getRequest()->getParam('_currency'))):
        if($this->getRequest()->getParam('_currency')==Mage::app()->getStore()->getCurrentCurrencyCode()){

        }else{
            Mage::app()->getStore()->setCurrentCurrencyCode($this->getRequest()->getParam('_currency'));
        }
endif;

before code unset($data['_store_to_url']); Modify function setRouteParams

public function setRouteParams(array $data, $unsetOldParams = true)
{
    if (isset($data['_type'])) {
        $this->setType($data['_type']);
        unset($data['_type']);
    }

    if (isset($data['_store'])) {
        $this->setStore($data['_store']);
        unset($data['_store']);
    }

    if (isset($data['_forced_secure'])) {
        $this->setSecure((bool)$data['_forced_secure']);
        $this->setSecureIsForced(true);
        unset($data['_forced_secure']);
    } elseif (isset($data['_secure'])) {
        $this->setSecure((bool)$data['_secure']);
        unset($data['_secure']);
    }

    if (isset($data['_absolute'])) {
        unset($data['_absolute']);
    }

    if ($unsetOldParams) {
        $this->unsetData('route_params');
    }

    $this->setUseUrlCache(true);
    if (isset($data['_current'])) {
        if (is_array($data['_current'])) {
            foreach ($data['_current'] as $key) {
                if (array_key_exists($key, $data) || !$this->getRequest()->getUserParam($key)) {
                    continue;
                }
                $data[$key] = $this->getRequest()->getUserParam($key);
            }
        } elseif ($data['_current']) {
            foreach ($this->getRequest()->getUserParams() as $key => $value) {
                if (array_key_exists($key, $data) || $this->getRouteParam($key)) {
                    continue;
                }
                $data[$key] = $value;
            }
            foreach ($this->getRequest()->getQuery() as $key => $value) {
                $this->setQueryParam($key, $value);
            }
            $this->setUseUrlCache(false);
        }
        unset($data['_current']);
    }

    if (isset($data['_use_rewrite'])) {
        unset($data['_use_rewrite']);
    }

    if (isset($data['_store_to_url']) && (bool)$data['_store_to_url'] === true) {
        if (!Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL, $this->getStore())
            && !Mage::app()->isSingleStoreMode()
        ) {
            $this->setQueryParam('___store', $this->getStore()->getCode());
        }
    }

    /* add by amit bera */
if(!is_null($this->getRequest()->getParam('_currency'))):
    if($this->getRequest()->getParam('_currency')==Mage::app()->getStore()->getCurrentCurrencyCode()){

    }else{
    Mage::app()->getStore()->setCurrentCurrencyCode($this->getRequest()->getParam('_currency'));
    }
endif;
/* end by amit bera */


    unset($data['_store_to_url']);

    foreach ($data as $k => $v) {
        $this->setRouteParam($k, $v);
    }

    return $this;
}

Let me know ,if you have any issue.

Related Topic