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 6 replies, has 2 voices.

Last updated by Shekhar Bhandari 3 months, 3 weeks ago.

Assigned support staff: Shekhar Bhandari.

Author Posts
June 27, 2019 at 3:52 pm #4101417

jenniferH-13

Tell us what you are trying to do?
I need to convert the price of the shopping cart with base currency as USD into whatever the currency switcher is currently set to, with foreign currencies set manually, not translated.

Is there any documentation that you are following?
The various forums and support topics online. I don't know how to access the functions API for the $woocommerce_wpml->multi-currency object.

Is there a similar example that we can see?
Not really.

What is the link to your site?
hidden link

June 27, 2019 at 4:04 pm #4101475

jenniferH-13
	$woocommerce->cart->set_quantity( $cart_item_key, $cart_quantity );
	$woocommerce->cart->set_session();
	$woocommerce->cart->calculate_totals();
	$totals = $woocommerce->cart->cart_contents_total;
	$new_totals = $woocommerce_wpml->multi_currency->formatted_price( $totals );

In the above code, $totals is in USD but the user has selected CAN on the front end currency switcher. This function is executed on the back end via AJAX and should return the appropriate amount, formatted for the appropriate currency if not USD. The last line currently generates a fatal error. I've tried some other functions as well and nothing works.

June 28, 2019 at 3:50 am #4104283

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Welcome to WPML support. I'd be assisting you further on this issue.

Can I ask you why you want to use the PHP method while the GUI option is available? Can you try to add the currency and price from WooCommerce Multilingual Multicurrency settings and let me know if that helps.

I just checked the https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/ and couldn't find any hooks for Multi-Currency Feature right now.

I encourage you to request a feature for the hook from here.
https://wpml.org/suggest-a-new-feature-for-wpml/

Look forward to your reply.

Thanks

June 28, 2019 at 12:43 pm #4107435

jenniferH-13

The workflow works like this:
1) customer chooses a program/product (this are outdoor children activities)
2) customer goes to checkout page (/checkout)
3) customer logs in and enters data about their children (still on /checkout)
4) customer chooses their preferred currency if it's not USD
5) customer enters a coupon code if they have one
6) customer uses checkboxes to select which children they want to register for the program (essentially program/product quantity)
7) customer uses the PayPal form to checkout

So during step 6, each time a checkbox is checked or unchecked there is an ajax call to the server to recalculate the current cart total based on number of children selected and applied coupon codes. What I've been able to do thus far is get an accurate total with coupon discount applied in base currency USD by using the original code I posted, OR get an accurate foreign currency cart total by using the code below:

	$multi_currency = $woocommerce_wpml->get_multi_currency();
	$multi_currency->prices->prices_init();
	$price_converted = $multi_currency->prices->get_product_price_in_currency( $product_id );
	$total = $price_converted*$cart_quantity;

Any other feedback is appreciated.

July 1, 2019 at 3:42 am #4115507

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hi there,

The customer uses checkboxes to select which children they want to register for the program (essentially program/product quantity)

Which plugin are you using for this option? or are you using some custom code along with WooCommerce to achieve this?

Since custom programming work is beyond the scope of our support, I suggest you follow the below steps to see if that helps, I haven't tested this so please test it properly before use.
- Get all the multi-currencies available along with its rates

global $woocommerce_wpml;
$multi_currency = $woocommerce_wpml->get_multi_currency();
$AED_rate = $multi_currency->currencies['AED']['rate'];

- Once the total is confirmed using the rates to get exact results according to your choice.

Providing further details help for custom code is outside the scope of support, at this point I would suggest you consider contacting one of our certified partners from this link:
http://wpml.org/documentation/support/wpml-contractors/

Before doing so please have a look also here:
http://wpml.org/documentation/support/wpml-contractors/guidelines-for-people-looking-for-consulting-help/

You will get the custom assistance you need to get on with your project.

Thanks

July 3, 2019 at 3:51 pm #4138039

jenniferH-13

I don't think the issue here has anything to do with custom code, rather I think the solution involves making WPML apply coupon codes. The following code gets the default currency cart total with coupon codes applied:

$woocommerce->cart->calculate_totals();
$totals = $woocommerce->cart->cart_contents_total;

Is there a way to set the currency to use when calculating cart totals?

The following code gets the price of the product in the desired currency (CAD):

	$multi_currency = $woocommerce_wpml->get_multi_currency();
	$multi_currency->prices->prices_init();
	$price_converted = $multi_currency->prices->get_product_price_in_currency( $product_id );

Is there a way to get the coupons to apply to the above code to get the proper converted price total?

Thanks.

July 4, 2019 at 3:20 am #4140033

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

To be able to help you further, I would need to test this once on my test site.

Being so, can you provide me step to step guide to replicate the issue (From graphical user interface) without using custom codes, so I can replicate the issue on a fresh new install and help you further?

Look forward to your reply.

Thanks

The topic ‘[Closed] What is the PHP code to format a price using the current currency?’ is closed to new replies.