April 18, 2016

Prestashop set Currency depending on the language (Prestashop version 1.6)

UPDATE
Based on the prestashop forum:
https://www.prestashop.com/forums/topic/52019-change-currency-automatically-when-switching-from-one-language-to-another/?p=1834832
Find ChangeCurrencyController.php and change this:
$this->context->cookie->id_currency = (int)$currency->id;

to:

$this->context->cookie->id_currency = (int)$currency->id;
$this->context->cookie->id_currency_changed = 1;

Find Tools.php and in function "switchLanguage" find and change line:

$language = new Language($id_lang);


to:


$language = new Language($id_lang);
            
            // added for hard currency linked switch
            switch($id_lang) {
                case 1: //if lang_id that's changed to currency id = 1 than...
                    Tools::switchCurrencyLinked(1);
                    break;
                case 2:
                    Tools::switchCurrencyLinked(2);
                    break;
                case 3: //and so on ....
                    Tools::switchCurrencyLinked(3);
                    break;
               }


Immediately after end of function public static function switchLanguage insert new function:

/**
     * Change cookie currency with Language when hard currency linked switch - janoo
     *
     */
     public static function switchCurrencyLinked($cookieLanguage)
    {
        global $cookie;

        if (! (int)$cookie->id_currency_changed)
        {
            $cookie->id_currency = $cookieLanguage;
            Tools::setCurrency($cookie);
        }
        else
        {
            $cookie->id_currency_changed = 0;
        }
    }



Credits goes to Devein, Chris2407 and janoo, Thanks!



OLD VERSION: In Prestashop Back Office, find ID for each currency and each language you want to use, (Language ID you can find in Back Office at "Localization -> Languages -> Edit:English" and in url you can see parameter "&id_lang=1&", the same for Currency)
than edit
classes/controller/FrontController.php in your Prestashop folder, find
$currency = Tools::setCurrency($this->context->cookie);
and add the following (so at the end it would be like this):
         if ($this->context->cookie->id_lang == 1){
           $this->context->cookie->id_currency = 2;
         }else{
           $this->context->cookie->id_currency = 1;
         }

        $currency = Tools::setCurrency($this->context->cookie);
Number "1" should be your Prestahop language ID (number "1" here is just an example), also "2" and "1" should be your Prestahop Currency ID.
This works for Prestashop version 1.6.

No comments:

Post a Comment