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 pleaseIf 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.
Quote from AMGV on February 13, 2024, 22:03Hello,
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,
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
Quote from Pablo Borysenco on February 14, 2024, 11:10Hello
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
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
Quote from AMGV on February 14, 2024, 16:50Hi 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:function verifica_discount_cos() {
global $WOOCS; // Ensure the global variable is defined to use the Currency Switcher// Get the cart subtotal in the shop's default currency (EUR)
$subtotal_cos = WC()->cart->subtotal;// Define discount levels in EUR and display the equivalent in 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.']
];// Check each discount level
foreach ($niveluri_discount as $nivel) {
if ($subtotal_cos < $nivel['limita']) {
$diferenta_eur = $nivel['limita'] - $subtotal_cos;// Convert the difference using the woocs_exchange_value function
$diferenta_ron = $WOOCS->woocs_exchange_value($diferenta_eur);// Convert the limit for accurate comparison
$limita_ron = $WOOCS->woocs_exchange_value($nivel['limita']);// Display the notice if the limit is not exceeded
if ($subtotal_cos < $limita_ron) {
wc_print_notice(sprintf($nivel['mesaj'], $diferenta_eur, round($diferenta_ron, 2)), 'notice');
}
break; // Exit the loop after displaying the first relevant message
}
}
}add_action('woocommerce_before_cart', 'verifica_discount_cos');
Using the same values as in the previous example, when we added a product worth 66 EUR to the cart. The message that is displayed now is “Add products worth €184.00 (approximately 184 RON) to get a 30% discount." As you can see, the conversion from EUR to RON is not correct. If we display the prices in RON, the message changes into:"Add 121.68 EUR (approximately 605.3 RON) to get a 40% discount." Now the conversion is correct but it is not the correct amount, it should still be €184.00 and a discount of 30% and not 40.
Can you help us understand where we are going wrong?
Givan
Hi Pablo,
Thank you for your answer.
function verifica_discount_cos() {
global $WOOCS; // Ensure the global variable is defined to use the Currency Switcher
// Get the cart subtotal in the shop's default currency (EUR)
$subtotal_cos = WC()->cart->subtotal;
// Define discount levels in EUR and display the equivalent in 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.']
];
// Check each discount level
foreach ($niveluri_discount as $nivel) {
if ($subtotal_cos < $nivel['limita']) {
$diferenta_eur = $nivel['limita'] - $subtotal_cos;
// Convert the difference using the woocs_exchange_value function
$diferenta_ron = $WOOCS->woocs_exchange_value($diferenta_eur);
// Convert the limit for accurate comparison
$limita_ron = $WOOCS->woocs_exchange_value($nivel['limita']);
// Display the notice if the limit is not exceeded
if ($subtotal_cos < $limita_ron) {
wc_print_notice(sprintf($nivel['mesaj'], $diferenta_eur, round($diferenta_ron, 2)), 'notice');
}
break; // Exit the loop after displaying the first relevant message
}
}
}
add_action('woocommerce_before_cart', 'verifica_discount_cos');
Using the same values as in the previous example, when we added a product worth 66 EUR to the cart. The message that is displayed now is “Add products worth €184.00 (approximately 184 RON) to get a 30% discount." As you can see, the conversion from EUR to RON is not correct. If we display the prices in RON, the message changes into:"Add 121.68 EUR (approximately 605.3 RON) to get a 40% discount." Now the conversion is correct but it is not the correct amount, it should still be €184.00 and a discount of 30% and not 40.
Can you help us understand where we are going wrong?
Givan
Quote from Pablo Borysenco on February 15, 2024, 10:50Hello 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;
}
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;
}
Quote from AMGV on February 15, 2024, 16:17Thanks for your help, we will contact a developer.
Thanks for your help, we will contact a developer.