PluginUs.Net - Business Tools for WooCommerce and WordPress

[realize your idea - make your dreams come true]

Support Forum

You need to log-in to create request (topic) to the support

Product Add ons not giving updated currency price

The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page please
If you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.
12

Hi,

The WooCommcer plugin for addons is not updating, this is the plugin https://woocommerce.com/products/product-add-ons

Is there a way to get this working?

I've tried the following hook and it worked to update the dropdown pricing, but the display price for the cart display is still wrong. If I add it to cart, the side cart is correct, its just a display issue.

would be good to get this fixed natively

see example at https://shop.scanimals.net/index.php/product/crystal-dragon/?currency=USD

 

code snippet as below

add_filter('woocommerce_product_addons_option_price', 'convert_addon_price_with_type_handling', 10, 3);
function convert_addon_price_with_type_handling($price, $addon, $cart_item) {
global $WOOCS;

// Ensure the add-on has a price set and retrieve the add-on price specifically
if (isset($addon['price']) && is_numeric($addon['price'])) {
$addon_price = floatval($addon['price']); // Ensure this is a float value

// Get the current currency and rate
$current_currency = $WOOCS->current_currency;
$currencies = $WOOCS->get_currencies();
$currency_rate = $currencies[$current_currency]['rate'];

// Convert the add-on price using the current currency rate
$converted_addon_price = $addon_price * $currency_rate;

// Log for debugging
error_log("Original Add-On Price:" . $addon_price);
error_log("Converted Add-On Price:" . $converted_addon_price);
error_log("Currency Rate:" . $currency_rate);

// Return the converted price formatted with two decimals
return '<span class="addon-price-converted">' . number_format($converted_addon_price, 2) . '</span>';
}

return $price; // Return the original price if no valid add-on price is found
}

add_filter('woocommerce_add_cart_item', 'apply_currency_conversion_to_addon_cart_items', 10, 1);
function apply_currency_conversion_to_addon_cart_items($cart_item) {
global $WOOCS;

if (isset($cart_item['addons'])) {
foreach ($cart_item['addons'] as &$addon) {
if (isset($addon['price']) && is_numeric($addon['price'])) {
$addon_price = floatval($addon['price']); // Ensure this is a numeric value

// Get the current currency and rate
$current_currency = $WOOCS->current_currency;
$currencies = $WOOCS->get_currencies();
$currency_rate = $currencies[$current_currency]['rate'];

// Convert the add-on price using the current currency rate
$converted_addon_price = $addon_price * $currency_rate;

// Update the add-on price in the cart item
$addon['price'] = number_format($converted_addon_price, 2);
}
}
}

return $cart_item;
}

Hello

would be good to get this fixed natively - Yes, it would be great, but the author of this third-party plugin should enter the code:

 

In file wp-content\plugins\woocommerce-product-addons\includes\class-wc-product-addons-cart.php add next code: https://share.pluginus.net/image/i20240531181742.png

if ($addon['price'] AND class_exists('WOOCS')) {
global $WOOCS;
$currrent = $WOOCS->current_currency;
if ($currrent != $WOOCS->default_currency AND $WOOCS->is_multiple_allowed) {
$currencies = $WOOCS->get_currencies();
$rate = $currencies[$currrent]['rate'];
$addon['price'] = $addon['price'] * $rate;
}
}

Also in the same file add code: https://share.pluginus.net/image/i20240531182106.png

if (class_exists('WOOCS')) {
global $WOOCS;
$currrent = $WOOCS->current_currency;
if ($currrent != $WOOCS->default_currency AND $WOOCS->is_multiple_allowed) {
$currencies = $WOOCS->get_currencies();
$rate = $currencies[$currrent]['rate'];
$addon_price = $addon_price * $rate;
}
}

In file wp-content\plugins\woocommerce-product-addons\includes\class-wc-product-addons-display.php add next code: - https://share.pluginus.net/image/i20240603113834.png

$suffix = ''; //woocs fix
$woocs_is_multiple = 0;
if (class_exists('WOOCS')) {
global $WOOCS;
$woocs_is_multiple = $WOOCS->is_multiple_allowed;
}

And on the same file add code: - https://share.pluginus.net/image/i20240603113927.png

'woocs_is_multiple' => $woocs_is_multiple

In file wp-content\plugins\woocommerce-product-addons\assets\js\frontend/addons.js add next code: - https://share.pluginus.net/image/i20240531183303.png

var woocs_exists = true;
try {
if (woocs_current_currency)
woocs_exists = true;
} catch (e) {
woocs_exists = false;
}
if (woocs_exists) {
if (woocs_current_currency != undefined AND woocs_current_currency['rate'] != undefined AND woocommerce_addons_params.woocs_is_multiple == 0) {
self.base_price = self.base_price * woocs_current_currency['rate'];
}
}
And on the same file: - https://share.pluginus.net/image/i20240531183404.png
var woocs_exists = true;
try {
if (woocs_current_currency)
woocs_exists = true;
} catch (e) {
woocs_exists = false;
}
if (woocs_exists) {
if (woocs_current_currency != undefined AND woocs_current_currency['rate'] != undefined AND addon_cost != undefined) {
addon_data.cost = addon_data.cost * woocs_current_currency['rate'];
addon_data.cost_raw = addon_data.cost_raw * woocs_current_currency['rate'];
}
}

and into file functions.php of the current WordPress theme add next code:

add_filter('woocommerce_product_addons_option_price', function ($price_html, $option, $i, $type) {

if ($price_html AND class_exists('WOOCS')) {
global $WOOCS;

$option_price = !empty($option['price']) ? $option['price'] : '';
$option_price_type = !empty($option['price_type']) ? $option['price_type'] : '';
$price_prefix = 0 < $option_price ? '+' : '';
$price_type = $option_price_type;
$price_raw = apply_filters('woocommerce_product_addons_option_price_raw', $option_price, $option);
$price_raw = $WOOCS->woocs_exchange_value($price_raw);
$price_html = '(' . $price_prefix . wc_price(WC_Product_Addons_Helper::get_product_addon_price_for_display($price_raw)) . ')';
}

return $price_html;
}, 20, 4);

Wow, that was a lot of changes needed, I have logged to the plugin owner asking them to implement.

 

But I'm in the same place as my previous snippet; please try https://shop.scanimals.net/index.php/product/crystal-dragon/?currency=USD

Dropdown has correct value, but the display has the wrong value, see https://ibb.co/QcSFrWm

Hello

I checked your site, it looks like you didn't add the code to the JS file - https://share.pluginus.net/image/i20241021121317.png

Odd, I had made the change and cleared the cache on woocommerce, but for some reason Cloudflare didnt see it even after days.

I have purged cache at cloudflare and the javascript changes are now showing.

Is the javascript supposed to just remove the addons display area as its now not showing at all.

User can obviously see the price in the dropdown, so thats ok, but I guess not what I was originally anticipating.

At least it gets an add to cart for user to see price and we get the chance to re-target the user in meta

Hello

Of course not! This JS code converts the amount.

If it doesn't work then you didn't do it correctly.

 

I found the issue, your code had AND where I had to replace it with && in 4 places.

Updating this for anyone else that needs this code and encase I need to come back.

There is an update for add-ons and I havent done it as I just got this working lol

 

Hello

Great!

looks like I spoke too soon.

While everything works nicely now and prices update if I select the currency; when a person initially loads the page its always in AUD, if they select their currency then everything is fine.
I tried disabling the cache/accelerator and cloudflare , now every time it defaults to USD
I Re-enabled the cache and again it default to AUD.
Disable cache and confirm defaults to USD again

My welcome currency is set to NZD
I have set GeoIP countries NZD for New Zealand AUD for Australia CAD for Canada AED for UAE and then all other countries for USD.
I have verified my IP is showing as New Zealand and is static so this isn't the cause
I have Selected currency in Schema.org enabled

I use [woocs] as any style causes issues where it can randomly change the currency or takes a delay for the correct currency to show.

I have been using Memcache and have just switched to transient.
It now shows NZD as base currency

With cache disabled I tested from 3 countries and it shows correct currency

I re-enabled memcache and tested 3 countries again via https://geotargetly.com/geo-browse and they show correctly for each currency

I enabled"I have a cache" and then USA shows USD but so do all the others except Australia shows AUD
So I tick prices pre-load with Ajax; now random results for different sites and USD for me in New Zealand on laptop and iphone

Perhaps you can tell me so I can relay to cache provider which javascript need to load at what point for this to actually work.
I've been missing out on lots of sales and would really like to get this working please, but it seems even without cache its causing issues for geoip

I think I got it to work well using the following code snippet, but then when I enable the cache it no longer works :/

add_action('template_redirect', 'append_currency_based_on_maxmind_geoip');

function append_currency_based_on_maxmind_geoip() {
error_log("append_currency_based_on_maxmind_geoip function has started.");

// Ensure WooCommerce is loaded and the session has started
if (class_exists('WooCommerce') && WC()->session) {
// Check if currency has already been initialized in this session
if (!WC()->session->get('currency_initialized')) {
error_log("WooCommerce session and class loaded successfully.");

// Fetch user's IP address for logging
$user_ip = $_SERVER['REMOTE_ADDR'];
error_log("User IP Address:" . $user_ip);

// Use WooCommerce's built-in geolocation
$geo_info = WC_Geolocation::geolocate_ip();

// Check if country information is available
if (!empty($geo_info['country'])) {
$user_country = $geo_info['country'];
error_log("Detected user country:" . $user_country);

// Define currency parameters based on the user's location
$currency_map = [
'US' => 'USD',
'AU' => 'AUD',
'NZ' => 'NZD',
'CA' => 'CAD',
'AE' => 'AED'
];

// Determine currency or default to USD
$currency_param = isset($currency_map[$user_country]) ? $currency_map[$user_country] : 'USD';
error_log("Currency parameter to be appended:" . $currency_param);

$current_url = $_SERVER['REQUEST_URI'];

// Only set currency if none exists in the URL
if (strpos($current_url, 'currency=') === false) {
$separator = strpos($current_url, '?') === false ? '?' : '&';
$new_url = $current_url . $separator . 'currency=' . $currency_param;
error_log("Redirecting to URL:" . $new_url);

// Set session flags to prevent future redirects
WC()->session->set('currency_initialized', true);

// Redirect to the new URL with currency parameter
wp_redirect($new_url);
exit;
} else {
error_log("Currency already set in URL, no redirect needed.");
// Mark currency as initialized to prevent future redirects
WC()->session->set('currency_initialized', true);
}
} else {
error_log("Country information not available via WooCommerce GeoIP.");
}
} else {
// Fetch user's IP address for logging
$user_ip = $_SERVER['REMOTE_ADDR'];
error_log("Currency already initialized for this session, no action taken for" . $user_ip);
}
}
}

does your code still require this snippet?

when I disable the snippet then the changes you had me make dont appear to work

I have turned the cache off completely

 

add_filter('woocommerce_product_addons_option_price', function ($price_html, $option, $i, $type) {

if ($price_html AND class_exists('WOOCS')) {
global $WOOCS;

$option_price = !empty($option['price']) ? $option['price'] : '';
$option_price_type = !empty($option['price_type']) ? $option['price_type'] : '';
$price_prefix = 0 < $option_price ? '+' : '';
$price_type = $option_price_type;
$price_raw = apply_filters('woocommerce_product_addons_option_price_raw', $option_price, $option);
$price_raw = $WOOCS->woocs_exchange_value($price_raw);
$price_html = '(' . $price_prefix . wc_price(WC_Product_Addons_Helper::get_product_addon_price_for_display($price_raw)) . ')';
}

return $price_html;
}, 20, 4);

 

 

Hello

Please  delete  your custom code - https://share.pluginus.net/image/i20241029113336.png

Read this  - https://currency-switcher.com/using-geolocation-causes-problems-doesnt-seem-to-work-for-me

Yes I discovered today after about 5hrs testing that currency kept changing.

I had incognito safari and chrome and iPhone and all devices kept changing.

I realised that when one device changed it they all changed currency so bound by IP

I validated this with a wget of currency=USD and all devices refresh browser from same IP with currency being USD

Yes.  Please  try  to use - https://share.pluginus.net/image/i20241029131027.png

to test locations try to use - https://locasnap.com/  OR https://www.locabrowser.com/

WooCommerce has come back to me with a permanent fix for this to be implemented by you, using hooks they have developed for currency support for their own currency plugin you can use. Please read their response below

 

I discussed internally with the dev team and they do have some good technical information to share regarding Product Add-Ons and Multi-Currency, as well as some feedback on how we approach community requests. Their message is below:

I had a look at the snippets these developers shared and I noticed that they suggested that the merchant edits the Product Add-Ons files and pastes the code directly there. This is not recommended, as every time Product Add-Ons is updated, all the compatibility code will be lost. Is this perhaps the issue they encountered and this is why they reached out to us?

As you correctly mentioned in an earlier message, we recently updated our compatibility with our own multi-currency solution that is offered via WooCommerce Payments. All the compatibility code between the two plugins is stored in the WooCommerce Payments side of things and therefore, if a compatibility was added with the multi-currency plugin they are using, it should be added on the multi-currency plugin.To achieve this, I would recommend for the developers of the multi-currency plugin to have a look at the integration class between Product Add-Ons and WooCommerce Payments, as they would probably need to use the same set of filters in their integration.

When developing an integration in the WordPress/WooCommerce ecosystem, the recommended way of doing this is by using the hooks that a plugin provides and adding the compatibility code in the callback functions of these hooks. Directly adding compatibility code inside another's plugin class is not recommended, as it makes the code more difficult to read and maintain. In case the developers of the multi-currency plugin have any specific, technical questions for us, we'd be happy to help!

Hello

Ok!  I'll pass this on to the developers.

Heya,

I'm wondering if your developers looked into this?

I've got an update for add-ons that I've held back applying as I would have to go manually update all the files again, so have been waiting to hopefully have this fix in place.

thanks

Hello

Unfortunately, we haven't tackled this task yet.

But the developers told me that this third-party plugin does not have enough hooks for adaptation without embedding code

the woocommerce developers said they would be happy to discuss a solution, I am hopeful your developers will reach out to them.

I suspect looking at the woocommerce multi currency and using the same method may work

Hello

Yes of course, our developers will take care of this.

12