Programmatically set default currency

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Kappa_Ghiffari
Posts: 2
Joined: 18 Nov 2021, 14:41

Programmatically set default currency

Post by Kappa_Ghiffari » 18 Nov 2021, 15:02

Good day,

We are working on creating an extension.
The extension supposed to be able handle
1. Checking user IP and use 3rd party API to detect country + currency code (done)
2. Customize Admin Panel to handle VAT in each product by different country/currencies (done)
3. Implement VAT rule + calculation (done, using https://aimeos.org/docs/latest/provider ... figuration)
4. Match user local currency to product object (stuck, need help)

So we need help on step 4.

Our idea to inject through product object (see code below)

Code: Select all

public function apply( \Aimeos\MShop\Product\Item\Iface $product ) <------- this product object
    {
        
    }
In the controller, we can do something like this to override currency in the manager instance and automatically adjust the price according to the currency variable value.

Code: Select all

$context = app('aimeos.context')->get();
$context->getLocale()->setCurrencyId('AUD')
$manager = MShop::create( $context, 'catalog' );
$category = $manager->get($id, ['text', 'media', 'product']);
However when I tried to do same logic on the apply method, it's not working as expected.

Can you advise how can we do this properly?
Any advice would be appreciated

Thanks

User avatar
aimeos
Administrator
Posts: 7895
Joined: 01 Jan 1970, 00:00

Re: Programmatically set default currency

Post by aimeos » 19 Nov 2021, 10:06

The way to want to do it is not going to work. Using a rule provider is a good idea to e.g. change the tax rate depending on the country which you've stored in the session after querying the 3rd party API but not for changing the currency.

First of all, you need a multi-currency setup with the available site/language/currency combinations in the Locale > Locale panel and the currency must be part of the URL. After you've done resolving the country and currency, you have to redirect the browser to the correct URL containing the currency so the right locale combination will be used. Then, you can use the rule provider to change the tax rate according to the country setting.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Kappa_Ghiffari
Posts: 2
Joined: 18 Nov 2021, 14:41

Re: Programmatically set default currency

Post by Kappa_Ghiffari » 19 Nov 2021, 10:30

Thanks for quick response and explanation
you have to redirect the browser to the correct URL containing the currency so the right locale combination will be used
I already finished on country & currency retrieval through 3rd party API , I just need to inject those values to browser URL.
how can I achieve this programmatically in aimeos?
where I should put/call my code so it can persists currency value through entire session without user needed to change it manually through selector?

Post Reply