Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by matthiasK-23 4 weeks, 1 day ago.

Assigned support staff: Yvette.

Author Posts
January 21, 2021 at 8:41 pm #7900965

matthiasK-23

I'm trying get info of a woocommerce product and the currency of a selected language programmatically in a custom script.

So first:

require_once('wp-load.php');

Then

global $sitepress;
$sitepress->switch_lang('nl');

On this site the only currency is €

But when I then do

echo get_woocommerce_currency_symbol();

it returns "£" - the currency of the main language

But

echo $sitepress->get_current_language();

returns "nl" rightfully.

What am I doing wrong?

Thank you!

January 22, 2021 at 5:26 pm #7908491

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Los_Angeles (GMT-08:00)

Hello

We only offer limited programming assistance. You may need to contact a WPML contractor here:
https://wpml.org/contractors/

It sounds like the call get_woocommerce_currency_symbol() is not passing through our hooks/filters and this is why you are getting the default symbol.

Please also take a look at the WPML API to see what other filters/hooks might be helpful for you.
https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

Do I understand that you are trying to get the default currency symbol assigned to a currency?
In this case, the language does not have anything to do with the currency being used by the site. A currency symbol is attached to a currency - not a language.

Perhaps you have multicurrency enabled on the site and are you associating a language with a currency there?

I don´t intend to offer coding assistance but just asking you to clarify what you are actually trying to do and why you think the language code is assoicated with a currency.

January 22, 2021 at 6:02 pm #7908707

matthiasK-23

Hi, thank you for your reply. I already had a look at all this, but there are some things I can't find out, or are simply not there.

So what I'm trying to do exactly:

This site is multilingual with en, fr, nl, nl-be, fr-be, fr-lu

Also this site has multicurrency enabled. "en" has £ and €. All other languages just feature €. En is the main language and apparently £ is the main currency.

Programmatically what I'm trying to do is on the shop front end I need to get product *and* shipping prices dynamically. So what I set up is a php script which returns those prices to an ajax call on the front page.

In this php script I'm switching trying to switch to a different language, because I thought that the currency is bound to the language. What's also a very tricky part is to get the shipping costs. Currently this code looks like this:

foreach ( WC()->cart->get_shipping_packages() as $package_id => $package ) {
	// Check if a shipping for the current package exist
	if ( WC()->session->__isset( 'shipping_for_package_'.$package_id ) ) {
		// Loop through shipping rates for the current package
		foreach ( WC()->session->get( 'shipping_for_package_'.$package_id )['rates'] as $shipping_rate_id => $shipping_rate ) {
			$rate_id     = $shipping_rate->get_id(); // same thing that $shipping_rate_id variable (combination of the shipping method and instance ID)
			$cost        = $shipping_rate->get_cost(); // The cost without tax
			$tax_cost    = $shipping_rate->get_shipping_tax(); // The tax cost
			if($rate_id == "flat_rate:1") { // Installation Service Rate ID
				$installation_service_price = $cost + $tax_cost;
			}
			if($rate_id == "flat_rate:2") { // Delivery Rate ID
				$shipping_price = $cost + $tax_cost;
			} 
		}
	}
}

Then I'm trying to get the prices of different products like this:

$productId = wc_get_product_id_by_sku($sku);
$product = wc_get_product($productId);
$regularPrice = $product->get_regular_price();
$salePrice = $product->get_sale_price();
$productPrice = $product->get_price();

No matter what language I switch to, it always returns the prices of en in £.

So what I found out with those WCML filters that I can use it like this

if($lang == "en") {
	$currency = "GBP";
	$currency_symbol = "£";
} else {
	$currency = "EUR";
	$currency_symbol = "€";
}
$productPrice = apply_filters( 'wcml_product_price_by_currency', $product_id, $currency );

But this only works for the product, not the shipping price.

The functions

get_woocommerce_currency();
get_woocommerce_currency_symbol();

always return GBP and £.

And that's why I contacted your support, because I thought there may be a similar function like

global $sitepress;
$sitepress->switch_lang($lang);

in WooCommerce Multilingual that allows me also to switch the currency of the current script session.

Thank you in advance for your help!

Best
Matthias

January 25, 2021 at 9:54 am #7919253

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: America/Los_Angeles (GMT-08:00)

Hello

I´m sorry , but your question is out of scope for our support forum.

Nevertheless, from what you wrote, it sounds like you are trying to replicate the same code that Woocommerce a does when calculating the shipping costs on the checkout page after shipping zones/classes are defined.

Screencast: hidden link

This code seems to pass through our filters for multicurrency. Please note that the calculation is not dependent on the active language but on the shipping zone and only the displayed amount is subject to active language.

In this case, I would suggest that you look at the code being used there. If you cannot find a filter/hook that meets your needs then I would venture to say that we don´t have an easy out-of-the-box solution ready for your use case and that you will have to develop one on your own.

January 26, 2021 at 10:41 am #7929719

matthiasK-23

So I found a workaround for me that seems to do the trick which is to switch to
the right currency based on the language like so

if($lang != "en") {
	add_filter('wcml_client_currency', 'wcml_custom_currency');
	function wcml_custom_currency($current){
		return 'EUR'; 
	}
}

And it seems that to get the shipping prices you need to have an active WooCommerce Cart Session, so on first page load I dynamically add a product to the cart and then remove it immediately. Probably not the nicest solution, but it works.

WC()->cart->add_to_cart( $productId );
WC()->cart->empty_cart();

Thanks