How To Change Store Currency From Addon

How can I change the current currency of the site from addon code?

Default currency changer uses GET request variable ‘currency’ to change the currency.

But I need to change it from the code according to some condition.

You can find how the currency is defined in the fn_init_currency function (app/functions/fn.init.php). Check for $secondary_currency.



We suggest you to use the fn_set_session_data('secondary_currencyC', 'USD'); code



Note that it should be executed before the fn_init_currency function is called

[quote name='eComLabs' timestamp='1438689367' post='225541']

You can find how the currency is defined in the fn_init_currency function (app/functions/fn.init.php). Check for $secondary_currency.



We suggest you to use the fn_set_session_data('secondary_currencyC', 'USD'); code



Note that it should be executed before the fn_init_currency function is called

[/quote]



Yes, I've already seen that but didn't know how to perform it before init.

Right know I got it to work by adding a new hook to 'fn_init_currency'.



But, I really don't like this … I don't want to touch anything in the original cs-cart code. So, if anyone has a better solution please share it.

Thanks

I have made quick overview and have not found any hooks. As a workaround you can set up currency in the nearest hook and redirect page to itself

[quote name='eComLabs' timestamp='1438716185' post='225627']

I have made quick overview and have not found any hooks. As a workaround you can set up currency in the nearest hook and redirect page to itself

[/quote]

The problem with this is that the '[color=#282828][font=arial, verdana, tahoma, sans-serif]fn_init_currency' function defines a constant for the currency.[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]So using any hook after it will give an error “constant already defined”.[/font][/color]

You will have to modify the init_currency function to check for your condition and do the appropriate setup.

[quote name='tbirnseth' timestamp='1438819164' post='225811']

You will have to modify the init_currency function to check for your condition and do the appropriate setup.

[/quote]

Basically I've done the same by adding a new hook to the function. But, doing what you are suggesting will force the new functionality even after deactivating or uninstalling the plugin.

Don't think a hook will work here. The addon has not yet been initialized.

[quote name='tbirnseth' timestamp='1438887162' post='225936']

Don't think a hook will work here. The addon has not yet been initialized.

[/quote]

I don't understand what you mean by “The addon has not yet been initialized” but it is already working fine.

I'm just not liking that I had to modify cs-cart core file for this so I'm looking for another way.

My mistake. Addons are initialized further up the init.php process so the hook would be registered.

You can request a hook be added here: Your Add-On Needs A New Hook In Cs-Cart. Post It Here. - Third-Party Add-ons - CS-Cart Community Forums

[quote name=‘tbirnseth’ timestamp=‘1438978547’ post=‘226044’]

My mistake. Addons are initialized further up the init.php process so the hook would be registered.

You can request a hook be added here: http://forum.cs-cart…__fromsearch__1

[/quote]



I’ve just requested the new hook.

Thanks :)

[quote name='Elsherif' timestamp='1438752310' post='225665']

The problem with this is that the '[color=#282828][font=arial, verdana, tahoma, sans-serif]fn_init_currency' function defines a constant for the currency.[/font][/color]

[color=#282828][font=arial, verdana, tahoma, sans-serif]So using any hook after it will give an error “constant already defined”.[/font][/color]

[/quote]



My solutions allows to exclude any changes in the core files. But in this case additional redirect is required. Usually such redirects are not visible for the customers

[quote name=‘eComLabs’ timestamp=‘1439282397’ post=‘226321’]

My solutions allows to exclude any changes in the core files. But in this case additional redirect is required. Usually such redirects are not visible for the customers

[/quote]



Although I don’t like the redirection idea (because I see it as a workaround more than a solution), I realize it is possibly the only way to achieve this (without the hook of course).

I will also check this. Thank you :)

You can always request that they leave the define and use a registry (runtime.cart_currency) instead. The define would remain in place for compatibility, but all internal uses would use the runtime registry value allowing addon developers to migrate over time.



But if they remove the define, all the addon developers are going to be mad at you for making a change that probably impacts most of them but with very little (if any) value returned to them.

[quote name=‘tbirnseth’ timestamp=‘1439406650’ post=‘226574’]

You can always request that they leave the define and use a registry (runtime.cart_currency) instead. The define would remain in place for compatibility, but all internal uses would use the runtime registry value allowing addon developers to migrate over time.



But if they remove the define, all the addon developers are going to be mad at you for making a change that probably impacts most of them but with very little (if any) value returned to them.

[/quote]



Wow … I most certainly don’t want that :D

I think the hook is just enough.