Wrong currency language applied when basket updates from JS

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!
kdim95
Advanced
Posts: 206
Joined: 26 Aug 2022, 12:17

Wrong currency language applied when basket updates from JS

Post by kdim95 » 07 Nov 2022, 14:45

Laravel framework version: 9.38.0
Aimeos Laravel version: * 2022.07.2
PHP Version: 8.1.12
Environment: Linux

The currency language is correct when the page is refreshed.
The currency language is wrong if a new product is added and the basket updates.

Example:
10.00 лв becomes BGN 10.00

So the problem is not only the currency language, but it seems that the string is not formatted through the translations.

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

Re: Wrong currency language applied when basket updates from JS

Post by aimeos » 08 Nov 2022, 18:47

The behavior occurs if the basket content is migrated from one locale to another. Make sure, your catalog list/detail and basket URLs all contain the same language/currency (or none).

Also check if you have the appropriate locales in the Locale > Locale panel in the admin backend and that your products all have texts and prices in that language resp. currency.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 206
Joined: 26 Aug 2022, 12:17

Re: Wrong currency language applied when basket updates from JS

Post by kdim95 » 16 Nov 2022, 10:00

I have installed a clean Aimeos to test this.

What I've done is include the bg locale with BGN currency in the Locales from this admin page:
/admin/default/jqadm/search/locale

I have turned on Bulgarian in the Languages from this admin page:
/admin/default/jqadm/search/locale/language

I have turned on Bulgarian Lev in the Currencies from this page:
/admin/default/jqadm/search/locale/currency

I have added both the Bulgarian currency and texts for the product that I'm adding to the cart.

The product currency is still not translated properly when added from the cart.
It normally appears as 00.00 лв, but when updated from the updateBasket() in basket-mini.js, it changes to BGN 00.00.

I am looking at this code inside basket-mini.js, is the currency supposed to be translated and formatted here?

Code: Select all

const formatter = new Intl.NumberFormat([], {
	currency: attr['order.base.currencyid'],
	style: "currency"
});

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

Re: Wrong currency language applied when basket updates from JS

Post by aimeos » 16 Nov 2022, 10:05

kdim95 wrote: 16 Nov 2022, 10:00 I am looking at this code inside basket-mini.js, is the currency supposed to be translated and formatted here?

Code: Select all

const formatter = new Intl.NumberFormat([], {
	currency: attr['order.base.currencyid'],
	style: "currency"
});
Yes, it's done by the browser in that case which is slightly different from the backend using the PHP Intl extension.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 206
Joined: 26 Aug 2022, 12:17

Re: Wrong currency language applied when basket updates from JS

Post by kdim95 » 16 Nov 2022, 11:59

In that case if the site is in one language and I have a different browser language, there will be a mismatch in the languages.
I'm looking into Intl, it looks like it accepts a language parameter, which I've set to languageid.

Code: Select all

const languageid = attr['order.base.languageid'];
const formatter = new Intl.NumberFormat(languageid, {
	currency: attr['order.base.currencyid'],
	style: "currency"
});
This doesn't seem ideal because there is a slight formatting mismatch with the one in Intl and the one in Aimeos.
The Intl format has an included dot (.) and the price looks like "00.00 лв." instead of "00.00 лв", but I've corrected it with .replace('.', '') , I think it would be best if the formatted price was included in the json response.

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

Re: Wrong currency language applied when basket updates from JS

Post by aimeos » 17 Nov 2022, 18:22

kdim95 wrote: 16 Nov 2022, 11:59 const formatter = new Intl.NumberFormat(attr['order.base.languageid'], {
currency: attr['order.base.currencyid'],
style: "currency"
});[/code]
Can you create a PR so the improvement also gets into the Aimeos core?

The price can't be returned already formatted by the JSON API because it has to return a standard float value so the API consumer can use it for calculation for example.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply