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

Problems with code for displaying messages in the cart page

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.

Hello,

Our company offers three levels of discount to its customers. We want to display messages on the shopping cart page depending on the order total, how many more products must be added to qualify for a certain discount.

We use two currencies in our store, EUR and RON. The base currency is EUR.

We have this custom code that displays the messages correctly when the prices are displayed in EUR, but if we choose to display the prices in RON, the message displays a wrong calculation.

function verifica_discount_cos() {
global $WOOCS; // Asigură-te că globalul este definit pentru a folosi Currency Switcher

// Obține sub-totalul coșului în EUR, moneda principală a site-ului
$subtotal_cos = WC()->cart->subtotal;

// Stabilește nivelurile de discount în EUR și afișează echivalentul în RON
$niveluri_discount = [
['limita' => 250, 'discount' => 30, 'mesaj' => 'Mai adaugă %s EUR (aproximativ %s RON) pentru a obține 30%% discount.'],
['limita' => 450, 'discount' => 40, 'mesaj' => 'Mai adaugă %s EUR (aproximativ %s RON) pentru a obține 40%% discount.']
];

// Verifică fiecare nivel de discount
foreach ($niveluri_discount as $nivel) {
if ($subtotal_cos < $nivel['limita']) {
$diferenta_eur = $nivel['limita'] - $subtotal_cos;

// Convertește diferența în RON folosind funcția convert_from_to_currency din WOOCS
$diferenta_ron = $WOOCS->convert_from_to_currency($diferenta_eur, 'EUR', 'RON');

wc_print_notice(sprintf($nivel['mesaj'], $diferenta_eur, round($diferenta_ron, 2)), 'notice');
break; // Ieși din buclă după afișarea primului mesaj relevant
}
}
}

add_action('woocommerce_before_cart', 'verifica_discount_cos');

As an example, when we added a product worth 66 EUR to the cart. According to the conditions in the cart page, a 20% discount was automatically applied. The message that is displayed is the following “Add products worth €184.00 to get a 30% discount." If we display the prices in RON, the message changes into this:"Add 121.58 lei to get a 40% discount." The conversion is not correct, 184 EUR is 915.4 RON and not 125.58. Moreover, the message mentions the 40% discount when it should refer to the 30% discount.

Can you help us? Do you have any idea how we can modify the code so that it renders the correct calculation/correct value in RON as well?

Thank you

Hello

https://share.pluginus.net/image/i20240214110603.png - This is not the correct code. Because you convert from EUR to RON regardless of the current currency

Better use this function - https://currency-switcher.com/function/woocs-woocs_exchange_value

And don’t forget to convert the limits you are comparing

Hi Pablo,

Thank you for your answer.

We tried to make changes to the code, but it still does not work correctly. This is the modified version of the initial code, the one above:

 

 

 

Hello Givan

Customization of third-party code is not included in support. You should hire a developer

Before performing mathematical operations, you should convert everything into one currency.

$subtotal_cos = WC()->cart->subtotal;   -  this function returns the amount in the current currency. and you should convert this into the base currency and after mathematical operations, convert the result into the current currency  OR convert all numbers in a mathematical operation to the current currency

Convert to base currency:

 

$currrent = $WOOCS->current_currency;

if ($currrent != $WOOCS->default_currency) {

$currencies = $WOOCS->get_currencies();

$rate = $currencies[$currrent]['rate'];

$your_any_price = $your_any_price / $rate;

}

 

 

 

 

 

Thanks for your help, we will contact a developer.

Welcome;)