0

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
  • What could be a solution? I need ANY kind of suffix at the end of the url's that disinuishes url links between CAD and USD. – philtak Jul 28 '14 at 17:36
  • Try adding ?currency=USD or GBP or any other currency that your website support and see if it converts. – philtak Jul 28 '14 at 17:51
  • I am trying to figure it out for the past week or so. Cant find the solution. Other sites that run other eCommerce platforms have this code fully working. – philtak Jul 28 '14 at 18:02
  • Serpyte, gentleman below mentioned the solution to our problem. How did it work in your case ? – philtak Jul 28 '14 at 21:15
  • I just realized the extension is no longer available. It got deleted. :( – philtak Jul 29 '14 at 23:10
2

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.

  • Amit, could you please provide a link of Magento store, to see how this correction functions ? – philtak Jul 28 '14 at 19:35
  • Sorry, i do not provide you link,i have test on my local system and it is working – Amit Bera Jul 28 '14 at 19:44
  • O, no problem. Just to clarify, you have mentioned the following: 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 I did not understand this part, could you please clarify. – philtak Jul 28 '14 at 19:48
  • yes,just before unset($data['_store_to_url']); – Amit Bera Jul 28 '14 at 19:51
  • Sorry, but I just got lost :( – philtak Jul 28 '14 at 20:00
0

Change

if(strpos($_SERVER["REQUEST_URI"],'?')){
$CurrentUrl=$_SERVER["REQUEST_URI"].'&_currency='   .$params['currency'];
}else{
    $CurrentUrl=$_SERVER["REQUEST_URI"].'?_currency='   .$params['currency'];
}
return $CurrentUrl;

with

if(strpos($_SERVER["REQUEST_URI"],'?')){
        $url_arr = parse_url($_SERVER["REQUEST_URI"]);
        $query = $url_arr['query'];
        $url = str_replace(array($query,'?'), '', $_SERVER["REQUEST_URI"]);
        $CurrentUrl=$url.'?currency='   .$params['currency'];
    }else{
        $CurrentUrl=$_SERVER["REQUEST_URI"].'?currency='   .$params['currency'];
    }
    return $CurrentUrl;

Working perfect for me.

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.