Jump to content

Load a currency by default other than the default currency?


Claudio

Recommended Posts

Hi guys,

 

I'm hoping that someone might be able to help me with this one, if at all possible.

My shop's default currency is USD (I need it to be in USD) and my main target market is from South Africa so most visitors require ZAR currency which is set up and configured. What I'd like to achieve without having to use a module for this purpose is for the website to automatically load ZAR currency for first-time visitors even though the default currency is USD and not ZAR. Is this possible by maybe changing some code?

 

Customers that do not require ZAR currency can of course change the currency to USD if they prefer, that option remains available via the currency selector.

 

The reason why I'd prefer not using a module for this purpose is that I have tried this before with 3 modules form the addon store and either they don't work correctly or end up slowing down the website when loading it for the first time for a new cuso5tmer. I need the website to quickly load ZAR currency by default even though the actual default currency set in Prestashop is USD.

 

Please tell me this is possible?

 

Thanks!

Edited by Claudio (see edit history)
Link to comment
Share on other sites

Hello Claudio.

 

while I can't tell you how to set ZAR as preferred currency in front office, I can tell you how to Force ZAR as your only currency in front office while having USD in back office.

**please make a backup before making any changes**
In this file , classes/Tools.php, search and edit the following lines and before we begin, please take note of your current ZAR currency ID found in the database under PREFIX_currency (example ps_currency) inside id_currency and if it differs from the one I've written below, which is 2, please change the number 2 to your ZAR currency ID:

From:

            $currency = Currency::getCurrencyInstance((int)$id_currency);	

To;

            $currency = Currency::getCurrencyInstance((int)$id_currency=2);

 

 

From:

            $currency = Currency::getCurrencyInstance((int)$cookie->id_currency);	

To:

            $currency = Currency::getCurrencyInstance(2);

 

 

From:

            $currency = Currency::getCurrencyInstance(Configuration::get('PS_CURRENCY_DEFAULT'));	

To:

            $currency = Currency::getCurrencyInstance(Configuration::get('id_currency'));

 

 

From:

                return Currency::getCurrencyInstance((int)$cookie->id_currency);	

To:

                return Currency::getCurrencyInstance(2);

 

Save and refresh your homepage.

the only problem with this solution is that is forces ZAR to be the default currency in your front office and your customers can not change it to USD, even though it might show in the currency block that USD is avaliable. Back office is unaffected though. I use this code to force SEK in my front office while having EUR in backoffice because my supplier provides me with euro prices and my customers buy in SEK.

 

 

Im working on another solution which loads the page with the preferred currency and writes it to  a cookie file but I have not figured out yet how to do it.

I will come back when I do, until then, have a great time.

 

Edited by rozhawni (see edit history)
  • Thanks 1
Link to comment
Share on other sites

  • 2 years later...
On 10/1/2018 at 10:36 PM, Claudio said:

Anyone?

 

I myself need this as well. My product supplier gives me rate of products in foreign currency. So, i want to add my price in that currency so that i do not have to change the final price if the currency conversion rate changes and it actually changes very often. And, it gets tougher to deal with the situation with the growing of your store. 

 

Have you managed any solution so far? Other than buying any module. Buying a module is okay. But, not a viable option of a new store or a store that doesn’t make much revenue out of it. 

Link to comment
Share on other sites

  • 3 weeks later...
On 10/6/2018 at 3:46 PM, rozhawni said:

Hello Claudio.

 

while I can't tell you how to set ZAR as preferred currency in front office, I can tell you how to Force ZAR as your only currency in front office while having USD in back office.

**please make a backup before making any changes**
In this file , classes/Tools.php, search and edit the following lines and before we begin, please take note of your current ZAR currency ID found in the database under PREFIX_currency (example ps_currency) inside id_currency and if it differs from the one I've written below, which is 2, please change the number 2 to your ZAR currency ID:

From:


            $currency = Currency::getCurrencyInstance((int)$id_currency);	

To;


            $currency = Currency::getCurrencyInstance((int)$id_currency=2);

 

 

From:


            $currency = Currency::getCurrencyInstance((int)$cookie->id_currency);	

To:


            $currency = Currency::getCurrencyInstance(2);

 

 

From:


            $currency = Currency::getCurrencyInstance(Configuration::get('PS_CURRENCY_DEFAULT'));	

To:


            $currency = Currency::getCurrencyInstance(Configuration::get('id_currency'));

 

 

From:


                return Currency::getCurrencyInstance((int)$cookie->id_currency);	

To:


                return Currency::getCurrencyInstance(2);

 

Save and refresh your homepage.

the only problem with this solution is that is forces ZAR to be the default currency in your front office and your customers can not change it to USD, even though it might show in the currency block that USD is avaliable. Back office is unaffected though. I use this code to force SEK in my front office while having EUR in backoffice because my supplier provides me with euro prices and my customers buy in SEK.

 

 

Im working on another solution which loads the page with the preferred currency and writes it to  a cookie file but I have not figured out yet how to do it.

I will come back when I do, until then, have a great time.

 

Hello,

does this work also for the multistore? I need to show only one currency different from default currency in one of the shops.

 

Thank you very much   Michal

Link to comment
Share on other sites

If anyone needs a solution to this problem and is willing to pay a small fee for a developer to implement the solution which is what I ended up having to do, then feel free to try this developer that I found on Freelancer which I used for this and some other mods. I can personally highly recommend their services;

https://www.freelancer.com/u/technovation1?ref_project_id=17876202

Link to comment
Share on other sites

  • 7 months later...

Its works for me:

Override Tools.php

\override\classes\Tools.php

With this i can use CAD only in frontend and USD for backend.

Used in Prestashop 1.7.7.4

Not conflict detected. Just need replace 2 lines. In my case i used 1 as id currency for CAD

 

<?php

class Tools extends ToolsCore
{

    /**
     * Set cookie currency from POST or default currency.
     *
     * @return Currency object
     */
    public static function setCurrency($cookie)
    {
        if (Tools::isSubmit('SubmitCurrency') && ($id_currency = Tools::getValue('id_currency'))) {
            /** @var Currency $currency */
            $currency = Currency::getCurrencyInstance((int) $id_currency);
            if (is_object($currency) && $currency->id && !$currency->deleted && $currency->isAssociatedToShop()) {
                $cookie->id_currency = (int) $currency->id;
            }
        }

        $currency = null;
        if ((int) $cookie->id_currency) {
            //$currency = Currency::getCurrencyInstance((int) $cookie->id_currency);//befoore
            $currency = Currency::getCurrencyInstance(1);//Force CAD for frontend.
        }
        if (!Validate::isLoadedObject($currency) || (bool) $currency->deleted || !(bool) $currency->active) {
            //$currency = Currency::getCurrencyInstance(Configuration::get('PS_CURRENCY_DEFAULT'));//before
            $currency = Currency::getCurrencyInstance(1);//Force CAD for frontend.
        }

        $cookie->id_currency = (int) $currency->id;
        if ($currency->isAssociatedToShop()) {
            return $currency;
        } else {
            // get currency from context
            $currency = Shop::getEntityIds('currency', Context::getContext()->shop->id, true, true);
            if (isset($currency[0]) && $currency[0]['id_currency']) {
                $cookie->id_currency = $currency[0]['id_currency'];

                return Currency::getCurrencyInstance((int) $cookie->id_currency);
            }
        }

        return $currency;
    }
}

 

Edited by caovillanueva (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...