VirtueMart Forum

VirtueMart 2 + 3 + 4 => Administration & Configuration => Topic started by: mratki on August 10, 2022, 11:16:10 AM

Title: Show price in two currencies
Post by: mratki on August 10, 2022, 11:16:10 AM
Hello,
I need a solution for displaying price in two currencies, EURO is coming to our country and my shop will have to display prices in two currencies at the same time, one below another.

I managed to add two prices in category view, product view and order page by adding code in /public_html/administrator/components/com_virtuemart/helpers/currencydisplay.php:

After line 340, I added:
$priceEUR = $price / 7.5345;
                $priceEUR = number_format($priceEUR, 2, ",", ".");

And after 355:
                    return '<div class="Price'.$name.$vis.'"><span class="vm-price-desc">'.$descr.'</span><span class="Price'.$name.'">'.$priceFormatted.'</span><br/><span class"price-eur">'.$priceEUR.' €</span></div>';

But It doesn't apply for a cart module, does somebody know the solution?

https://prnt.sc/V9ALzx6Ea_sK

Webpage: https://canivau.com/hr/shop
VM version: 4.0.6
PHP version: 8.0
Joomla: 3.9.11.

Regards,
Mateo
Title: Re: Show price in two currencies
Post by: pinochico on August 10, 2022, 12:07:04 PM
This is wrong code and wrong idea :)

You have to use internal core VM function ($currency->roundForDisplay) and add this code to sublayout price

You can see on our shop:
https://www.zelenazeme.cz

EUR + CZK
Title: Re: Show price in two currencies
Post by: Studio 42 on August 10, 2022, 14:45:07 PM
Pinocchio, your method doesnt work when you have price per quantity or variant, you have to change some javascripts too.

About your module, some third party module does not use VM Logic
To display correctly prices you should see this code in your modules
shopFunctionsF::renderVmSubLayout('prices',array('product'=>$this->product,'currency'=>$this->currency));
This use a file that you can overide in your template folder templates\TEMPLATE-NAME\html\com_virtuemart\sublayouts\
Original file is JOOMLA\components\com_virtuemart\sublayouts\prices.php
The lines to add :$price = $product->prices['SalesPrice'];
$product->prices['convertedPrice'] = $currency->roundForDisplay($price, 0,1.0,true)
echo $currency->createPriceDiv ('convertedPrice', 'COM_VIRTUEMART_PRODUCT_CONVERTEDPRICE', $product->prices);

I do not tested. But should be right
Title: Re: Show price in two currencies
Post by: Jumbo! on August 10, 2022, 18:04:21 PM
You can use the following codes to display the prices in Euro. It will also work on the checkout page and orders.

<?php
$euroCurrencyId 
47;
$vendorId       1;
$currencyEur    CurrencyDisplay::getInstance($euroCurrencyId$vendorId);

echo 
$currencyEur->priceDisplay($price$euroCurrencyId);
?>
Title: Re: Show price in two currencies
Post by: pinochico on August 10, 2022, 18:11:52 PM
To Patrick:

Yes, I see this RIGHT code in our module, sorry about your info :)


<div class="vm3pr-<?php echo $rowsHeights['price'?>">
<?php
echo 
shopFunctionsF::renderVmSubLayout('prices', array(
'product' => $product'currency' => $currency
));
?>

</div>


QuoteThis use a file that you can overide in your template folder templates

Yes, we use OVERRIDE in our template, sorry about your info :)


QuoteThe lines to add :

You are right and your code should be function, but see this code :)
Important is not function createPriceDIV, but function about I wrote:
$currency->roundForDisplay(xxxxxx)

Why?
Because we need in our shops not default html structure from function createPriceDiv, but other.

Then my idea is right.


I think basic idea is still right, your info is only add more notes:
- use sublayout price in tmpl for module
- in sublayout price create custom price by function roundForDisplay (better is use override in template)

And if people looking to code right, find this code self....
Title: Re: Show price in two currencies
Post by: pinochico on August 10, 2022, 18:15:03 PM
Jumbo,

I don't think so.

Why is so complicated?

In sublayout price is only one line and on one place.
Title: Re: Show price in two currencies
Post by: Jumbo! on August 10, 2022, 20:39:14 PM
Quote from: pinochico on August 10, 2022, 18:15:03 PM
Jumbo,

I don't think so.

Why is so complicated?

In sublayout price is only one line and on one place.

That's because if you simply use the sub-layout without creating a new instance of CurrencyDisplay with the correct currency, it will not get the correct Exchange Rate.

Test it yourself, and you will understand.
Title: Re: Show price in two currencies
Post by: pinochico on August 10, 2022, 21:45:05 PM
aha,
But I think I created new instalce in sublayout prices:


echo '<span class="price-locale"><div class="PricesalesPrice-locale vm-display vm-price-value"><span class="vm-price-desc">&nbsp;</span><span class="PricesalesPrice-locale">' . $currency->roundForDisplay($product->prices['product_price'],131,1,false,2) . ' ₽</span></div></span>';
echo '<span class="price-locale"><div class="PricesalesPrice-locale vm-display vm-price-value"><span class="vm-price-desc">&nbsp;</span><span class="PricesalesPrice-locale">' . $currency->roundForDisplay($product->prices['product_price'],199,1,false,2) . ' ₴</span></div></span>';


Basic price in shop is EUR,
two other prices are in RU rubles and UA hrivny

Or is my construct wrong?
Title: Re: Show price in two currencies
Post by: mratki on August 12, 2022, 12:44:40 PM
Quote from: Studio 42 on August 10, 2022, 14:45:07 PM
Pinocchio, your method doesnt work when you have price per quantity or variant, you have to change some javascripts too.

About your module, some third party module does not use VM Logic
To display correctly prices you should see this code in your modules
shopFunctionsF::renderVmSubLayout('prices',array('product'=>$this->product,'currency'=>$this->currency));
This use a file that you can overide in your template folder templates\TEMPLATE-NAME\html\com_virtuemart\sublayouts\
Original file is JOOMLA\components\com_virtuemart\sublayouts\prices.php
The lines to add :$price = $product->prices['SalesPrice'];
$product->prices['convertedPrice'] = $currency->roundForDisplay($price, 0,1.0,true)
echo $currency->createPriceDiv ('convertedPrice', 'COM_VIRTUEMART_PRODUCT_CONVERTEDPRICE', $product->prices);

I do not tested. But should be right

I'm getting syntax error, unexpected T_ECHO, which clearly I have no idea how to solve. Or I'm just trying to put it in the wrong place...

Regards,
M
Title: Re: Show price in two currencies
Post by: Studio 42 on August 12, 2022, 16:57:01 PM
$product->prices['convertedPrice'] = $currency->roundForDisplay($price, 0,1.0,true)
Add ;
Title: Re: Show price in two currencies
Post by: mratki on August 30, 2022, 14:06:30 PM
Quote from: Studio 42 on August 12, 2022, 16:57:01 PM
$product->prices['convertedPrice'] = $currency->roundForDisplay($price, 0,1.0,true)
Add ;

Can you help me a little bit more about it please? I would be grateful.. I mean, The syntax error is gone, but I still don't see the price in euros

Regards,
M
Title: Re: Show price in two currencies
Post by: mratki on August 31, 2022, 08:44:06 AM
Quote from: pinochico on August 10, 2022, 21:45:05 PM

echo '<span class="price-locale"><div class="PricesalesPrice-locale vm-display vm-price-value"><span class="vm-price-desc">&nbsp;</span><span class="PricesalesPrice-locale">' . $currency->roundForDisplay($product->prices['product_price'],131,1,false,2) . ' ₽</span></div></span>';
echo '<span class="price-locale"><div class="PricesalesPrice-locale vm-display vm-price-value"><span class="vm-price-desc">&nbsp;</span><span class="PricesalesPrice-locale">' . $currency->roundForDisplay($product->prices['product_price'],199,1,false,2) . ' ₴</span></div></span>';


This actually works, but not when you have price per quantity :/

Regards,
M
Title: Re: Show price in two currencies
Post by: mratki on August 31, 2022, 08:56:12 AM
Quote from: Jumbo! on August 10, 2022, 18:04:21 PM
You can use the following codes to display the prices in Euro. It will also work on the checkout page and orders.

<?php
$euroCurrencyId 
47;
$vendorId       1;
$currencyEur    CurrencyDisplay::getInstance($euroCurrencyId$vendorId);

echo 
$currencyEur->priceDisplay($price$euroCurrencyId);
?>


Unfortunately, it doesn't work for me. The price in € has been added, but it always shows me 0.00€

Regards,
M
Title: Re: Show price in two currencies
Post by: pinochico on August 31, 2022, 10:05:50 AM
QuoteThis actually works, but not when you have price per quantity

Is simple - change code as you wish :)

Title: Re: Show price in two currencies
Post by: mratki on August 31, 2022, 15:53:24 PM
Quote from: Jumbo! on August 10, 2022, 18:04:21 PM
You can use the following codes to display the prices in Euro. It will also work on the checkout page and orders.

<?php
$euroCurrencyId 
47;
$vendorId       1;
$currencyEur    CurrencyDisplay::getInstance($euroCurrencyId$vendorId);

echo 
$currencyEur->priceDisplay($price$euroCurrencyId);
?>


Here's the screenshot: https://prnt.sc/kqJV2ST7fqYl
Any ideas?

Regards,
Mateo
Title: Re: Show price in two currencies
Post by: Jumbo! on August 31, 2022, 16:51:59 PM
Have you defined/replaced the $price variable?
Title: Re: Show price in two currencies
Post by: mratki on September 05, 2022, 10:58:35 AM
Quote from: Jumbo! on August 31, 2022, 16:51:59 PM
Have you defined/replaced the $price variable?

I didn't think about it at all, my php knowledge is awfull, can you help me with the variable definition?

Regards,
Mateo
Title: Re: Show price in two currencies
Post by: Milbo on October 05, 2022, 23:59:09 PM
Quote from: Jumbo! on August 10, 2022, 18:04:21 PM
You can use the following codes to display the prices in Euro. It will also work on the checkout page and orders.

<?php
$euroCurrencyId 
47;
$vendorId       1;
$currencyEur    CurrencyDisplay::getInstance($euroCurrencyId$vendorId);

echo 
$currencyEur->priceDisplay($price$euroCurrencyId);
?>


Quote from: Jumbo! on August 10, 2022, 20:39:14 PM
Quote from: pinochico on August 10, 2022, 18:15:03 PM
Jumbo,

I don't think so.

Why is so complicated?

In sublayout price is only one line and on one place.

That's because if you simply use the sub-layout without creating a new instance of CurrencyDisplay with the correct currency, it will not get the correct Exchange Rate.

Test it yourself, and you will understand.

Absolutly right. You need one currency instance per currency. So if you want to display 2 currencies at the same time, you need to create two instances.