Jump to content

Edit History

senso321

senso321

On PrestaShop version: 1.7.8.0, this is how you would more or like to achieve the goal based on the original answer 👇

Keep in mind that you should take advantage of overrides when overriding default behaviors. Hence the code would go to overrides/classes/Tools.php

    /**
     * If necessary change cookie language ID and context language.
     *
     * @param Context|null $context
     *
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    public static function switchLanguage(Context $context = null)
    {
        if (null === $context) {
            $context = Context::getContext();
        }

        // On PrestaShop installations Dispatcher::__construct() gets called (and so Tools::switchLanguage())
        // Stop in this case by checking the cookie
        if (!isset($context->cookie)) {
            return;
        }

        if (
            ($iso = Tools::getValue('isolang')) &&
            Validate::isLanguageIsoCode($iso) &&
            ($id_lang = (int) Language::getIdByIso($iso))
        ) {
            $_GET['id_lang'] = $id_lang;
        }

        // Only switch if new ID is different from old ID
        $newLanguageId = (int) Tools::getValue('id_lang');

        if (
            Validate::isUnsignedId($newLanguageId) &&
            $newLanguageId !== 0 &&
            $context->cookie->id_lang !== $newLanguageId
        ) {
            $context->cookie->id_lang = $newLanguageId;
            $language = new Language($newLanguageId);
            if (Validate::isLoadedObject($language) && $language->active && $language->isAssociatedToShop()) {
                $context->language = $language;
            }
        }

        /** This is the switch we added to match language ID to a desired currency ID */
        switch($newLanguageId) {
            case 1: //if lang_id that's changed to is = 1 than...
                $context->cookie->id_currency = 2;  //change the currency too, the currency that has the id 2
                break;
            case 2: //and so on ....
                $context->cookie->id_currency = 1;
                break;
            case 3:
                $context->cookie->id_currency = 1;
                break;
            case 4:
                $context->cookie->id_currency = 4;
                break;
            default:
                $context->cookie->id_currency = 1;
        }
        
        Tools::setCookieLanguage($context->cookie);

        /** This is the function that actually sets the currency */
        Tools::setCurrency($context->cookie);
    }

 

senso321

senso321

On PrestaShop version: 1.7.8.0, this is how you would more or like to achieve the goal based on the original answer 👇

Keep in mind that you should take advantage of overrides when overriding default behaviors. Hence the code would go to overrides/classes/Tools.php

    /**
     * If necessary change cookie language ID and context language.
     *
     * @param Context|null $context
     *
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    public static function switchLanguage(Context $context = null)
    {
        if (null === $context) {
            $context = Context::getContext();
        }

        // On PrestaShop installations Dispatcher::__construct() gets called (and so Tools::switchLanguage())
        // Stop in this case by checking the cookie
        if (!isset($context->cookie)) {
            return;
        }

        if (
            ($iso = Tools::getValue('isolang')) &&
            Validate::isLanguageIsoCode($iso) &&
            ($id_lang = (int) Language::getIdByIso($iso))
        ) {
            $_GET['id_lang'] = $id_lang;
        }

        // Only switch if new ID is different from old ID
        $newLanguageId = (int) Tools::getValue('id_lang');

        if (
            Validate::isUnsignedId($newLanguageId) &&
            $newLanguageId !== 0 &&
            $context->cookie->id_lang !== $newLanguageId
        ) {
            $context->cookie->id_lang = $newLanguageId;
            $language = new Language($newLanguageId);
            if (Validate::isLoadedObject($language) && $language->active && $language->isAssociatedToShop()) {
                $context->language = $language;
            }
        }

        /** This is the switch we added to match language ID to a desired currency ID */
        switch($newLanguageId) {
            case 1: //if lang_id that's changed to is = 1 than...
                $context->cookie->id_currency = 2;  //change the currency too, the currency that has the id 2
                break;
            case 2: //and so on ....
                $context->cookie->id_currency = 1;
                break;
            case 3:
                $context->cookie->id_currency = 1;
                break;
            case 4:
                $context->cookie->id_currency = 4;
                break;
            default:
                $context->cookie->id_currency = 1;
        }
        
        Tools::setCookieLanguage($context->cookie);

        /** This is the function that actually sets the currency */
        Tools::setCurrency($context->cookie);
    }

 

×
×
  • Create New...