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

first item added to cart in wrong currency

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.

Hi,

I just tested our website from Australia (native shop is New Zealand). All prices show correctly in AUD, however when I add first item to cart it shows NZD.
Item shows $17.29 AUD on product page https://shop.scanimals.net/index.php/product/laughing-axolotl/ , when I add to cart it shows $16.52 NZD ($19 NZD with 15% GST removed)
Top right cart shows $16.52 , I click to another product page and suddenly cart at top right then shows $14.92, I open cart sidebar and it shows $14.92 AUD

So I can understand the $16.52 and $14.92 prices because this is no GST and then currency converted, but I need to understand why when first item added to cart it is showing wrong currency.

Hello

Please paste your license key here - https://share.pluginus.net/image/i20230222134241.png -> https://share.pluginus.net/image/i20230222134511.png

This may be an incompatibility with a third-party cart.

fair enough, we use funnelkit

With regards to the license part for private data, please tell me where to find it as I cant find an email from pluginus.net but I;ve already published the private data on another ticket so is on this board / my profile already.

To get  license  key - https://codecanyon.net/downloads -> https://c2n.me/49ZXJv0.png

 

done

Hello

Do you use custom code to switch currencies? I can't switch to AUD

Please check options - https://share.pluginus.net/image/i20250107105251.png  AND  https://share.pluginus.net/image/i20250107105318.png

So I can understand the $16.52 and $14.92 prices because this is no GST and then currency converted,  - The reason may be double conversion when a third-party plugin changes prices (product options plugin)

I only allow switch currencies on checkout page

Your first image is broken, there is nothing but blank?

I only allow switch currencies on checkout page - Are you using custom code?

fixed link - https://share.pluginus.net/image/i20250107105251.png

we are using cache but its cached per region. we do not wish to use these two settings.

could you take a look at page and see if you can see why its not working. As per below, currency_currency is AUD but when adding to the cart we see its set to NZD. do we need to perhaps invoke

var woocs_is_mobile = 0;
var woocs_special_ajax_mode = 0;
var woocs_drop_down_view ="no";
var woocs_current_currency = {"name":"AUD","rate":0.9033899903297424,"symbol":"$","position":"left","is_etalon":0,"hide_cents":0,"hide_on_front":0,"rate_plus":"","decimals":2,"separators":"0","description":"Australian Dollar","flag":"https:\/\/assets.scanimals.net\/wp-content\/uploads\/2024\/10\/au.png"};
var woocs_default_currency = {"name":"NZD","rate":1,"symbol":"$","position":"left","is_etalon":1,"hide_cents":0,"hide_on_front":0,"rate_plus":"","decimals":2,"separators":"0","description":"New Zealand Dollar","flag":"https:\/\/assets.scanimals.net\/wp-content\/uploads\/2024\/10\/nz.png"};
var woocs_redraw_cart = 1;
var woocs_array_of_get = '{}';
woocs_array_no_cents = '["JPY","TWD"]';
var woocs_ajaxurl ="https://shop.scanimals.net/wp-admin/admin-ajax.php";
var woocs_lang_loading ="loading";
var woocs_shop_is_cached =0;

To replicate go to https://shop.scanimals.net/index.php/product/enchanted-snake/ add to cart. you will see item in NZD. refresh page doesnt change the side cart price. but if you browse to another page and view cart its right in your currency; if you go back to https://shop.scanimals.net/index.php/product/enchanted-snake/ and view cart again its in correct currency.

So the issue appears to be that on the first item added to cart we need to somehow refresh the ajax again or something for the users currency.

would be good to get this fixed without adding the additional cache / ajax redraw which we had issues with.

It appears that when I add an item to cart and it calls /?wc-ajax=get_refreshed_fragments that its in NZD, but If I call /?wc-ajax=get_refreshed_fragments&currency=AUD this fixes things.

Is there a way to append the currency to the end of the ajax refresh? or what is it that does the"currency":"aud", on https://shop.scanimals.net/?wc-ajax=get_refreshed_fragments ?

I somehow need to ensure this is being set.

I have checked cookie data and see wp_woocs_session_1xxxxxxxx and it remains the same through the users entire transaction from cache page load to add to cart and change to another product page, which then eventually changes to currency":"aud

Please go to https://shop.scanimals.net/index.php/your-geolocation-result/ with dev tools and see console.
then refresh page and look at console.

When user hits the page I have javascript at bottom of the page to check for cookie, if doesnt exist then fetch custom code) see below

<script seraph-accel-crit="1">
document.addEventListener('DOMContentLoaded', function () {
// Helper function to get a cookie value
function getCookie(name) {
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
return match ? decodeURIComponent(match[2]) : null;
}

// Helper function to set a cookie
function setCookie(name, value, days) {
const expires = new Date();
expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = `${name}=${value}; expires=${expires.toUTCString()}; path=/; SameSite=Lax`;
}

// Fetch the currency from the AJAX endpoint if the cookie doesn't exist
if (!getCookie('wccs_init_currency')) {
console.log('No cookie found. Fetching currency...');
fetch('/wp-admin/admin-ajax.php?action=get_currency')
.then(response => response.json())
.then(data => {
if (data.success && data.data.currency) {
// Set the currency as a cookie
setCookie('wccs_init_currency', data.data.currency, 2);
console.log('Currency set:', data.data.currency);
} else {
console.error('Failed to retrieve currency from AJAX.');
}
})
.catch(error => console.error('Error fetching currency:', error));
} else {
console.log('Cookie found. No need to fetch currency.');
}

// Modify all AJAX requests with ?wc-ajax=
const originalFetch = window.fetch;
window.fetch = function (...args) {
let url = args[0];
const currency = getCookie('wccs_init_currency');

if (currency && typeof url === 'string' && url.includes('?wc-ajax=')) {
const urlObject = new URL(url, window.location.origin);
urlObject.searchParams.set('currency', currency);
args[0] = urlObject.toString();
console.log('Modified AJAX URL:', args[0]);
}

return originalFetch.apply(this, args);
};

// Intercept jQuery AJAX calls (if WooCommerce uses jQuery)
if (typeof jQuery !== 'undefined') {
jQuery.ajaxPrefilter(function (options) {
const currency = getCookie('wccs_init_currency');
if (currency && options.url.includes('?wc-ajax=')) {
const urlObject = new URL(options.url, window.location.origin);
urlObject.searchParams.set('currency', currency);
options.url = urlObject.toString();
console.log('Modified jQuery AJAX URL:', options.url);
}
});
}
});
</script>

Then is code snippet I have following to fetch users currency

add_action('wp_ajax_get_currency', 'ajax_get_currency');
add_action('wp_ajax_nopriv_get_currency', 'ajax_get_currency');

function ajax_get_currency() {
// Prevent caching
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

// Use the [woocs_show_current_currency] shortcode to determine the currency
$currency = do_shortcode('[woocs_show_current_currency text="" flag=0]');

// Sanitize the output to ensure a valid currency code
if (!empty($currency)) {
$currency = trim(strip_tags($currency)); // Remove HTML tags
$currency = html_entity_decode($currency); // Decode HTML entities like &nbsp;
$currency = preg_replace('/[^A-Z]/', '', strtoupper($currency)); // Keep only uppercase letters
$currency = substr($currency, 0, 3); // Get the first three letters
} else {
$currency = 'NZD'; // Default fallback
}

// Check if the cookie is already set and matches the current value
if (!isset($_COOKIE['wccs_init_currency']) || $_COOKIE['wccs_init_currency'] !== $currency) {
// Set the cookie for 2 days if the value is different
$cookie_expiration = time() + (2 * 24 * 60 * 60); // 2 days in seconds

// Set cookie with proper attributes
setcookie(
'wccs_init_currency',
$currency,
$cookie_expiration,
'/', // Path (root-level)
'', // Domain (use default domain)
is_ssl(), // Secure flag (true if SSL is enabled)
true // HttpOnly flag (prevents client-side script access)
);

// Ensure SameSite attribute for compatibility
header("Set-Cookie: wccs_init_currency={$currency}; Expires=" . gmdate('D, d-M-Y H:i:s T', $cookie_expiration) ."; Path=/; SameSite=Lax; Secure=" . (is_ssl() ? 'true' : 'false'));
}

// Return the currency as a JSON response
wp_send_json_success(['currency' => $currency]);
}

My question, is it possible to do this sooner, i.e. set wccs_init_currency at same time you set wp_woocs_session_xxxx ; this would mean user currency would be set sooner in cookie thus make sure first ajax call for ?wc-ajax=get_refreshed_fragments will allow for currency to be appended sooner.

We are using nginx to cache entire page and then cloudflare to cache everything (except checkout and dynamic ajax calls) and we rely on ajax wc-ajax=get_refreshed_fragments to update cart value and items.

Look forward to hearing from you.
Thanks
Barry

I may have fixed this with the current javascript, you may want it for future and perhaps it may help someone else.

https://assets.scanimals.net/wp-content/themes/woodmart-child/js/currency-set-cookie.js

Hello

Great! Thank you for cooperation!