cancel
Showing results for 
Search instead for 
Did you mean: 

How to get product's price in all allowed currencies?

How to get product's price in all allowed currencies?

I have a setup with two store views.

 

The first store view has base and the only one currency set to USD. The second one has 2 allowed currencies - USD and EUR. EUR is the default displayed one, USD is set as a base one.

 

All products' prices are set only in USD and the exchange rate for EUR is set to 0.75. Price of the product is let's say $14.

 

And my code goes:

// Store ID 2 - default currency EUR, base currency USD
// $product is instance of Magento\Catalog\Model\Product
$priceInfo = $product->getPriceInfo();
$price = $priceInfo->getPrice('regular_price')->getValue();

This code always fetch the correct price (10.50) in EUR. But I need both prices - in USD and in EUR, but I didn't find a way on how to pass currency to any getPrice method.

 

I tried to use Magento\Directory\Helper\Data to convert:

$helper->currencyConvert($price, 'EUR', 'USD');

But it gives me $13.99 when the real price in USD is $14 - so the calculation is wrong.

Do you know how can I get product's prices for both store currencies?

 

Thanks a lot!

13 REPLIES 13

Re: How to get product's price in all allowed currencies?

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceCurrencyObject = $objectManager->get('Magento\Framework\Pricing\PriceCurrencyInterface');
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); //instance of StoreManagerInterface
if ($store == null) {
	$store = $storeManager->getStore()->getStoreId(); //get current store id if store id not get passed
}
$rate = $priceCurrencyObject->convert($amount, $store, $currency);
$amount = $amount / $rate;
return $priceCurrencyObject->round($amount);

 @algolia


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to get product's price in all allowed currencies?

Thanks @Sunil Patel,

but unfortunately I'm not able to make it work. When I use your code I get wrong price for both EUR and USD.

Maybe the issue is when fetching the `$amount` to convert. Could you share the code how you get `$amount` from Product?

 

Thanks!

Re: How to get product's price in all allowed currencies?

@algolia

 

you can use 

$finalPriceAmt = $_product->getPriceInfo()->getPrice('final_price')->getValue();

Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to get product's price in all allowed currencies?

Thanks @Sunil Patel,

 

but the issue is that the

$finalPriceAmt = $_product->getPriceInfo()->getPrice('final_price')->getValue(); 

already gives me price in euros. But I'm not able to get the price in USD (which is the base currency). And when I convert it with 

$price = $priceCurrencyObject->convert($price, $store, $currency_code);

the price in USD is even lower ($6.99) then price in EUR (9.89e), but the real price in USD is $14.

 

I cannot get to that number.

Re: How to get product's price in all allowed currencies?

@algolia i make a one script and it is working fine for me.

 

make sure you imported rate in backend like EURO to USD rate

 

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
	
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); //instance of StoreManagerInterface
$priceCurrencyObject = $objectManager->get('Magento\Framework\Pricing\PriceCurrencyInterface');
$store = $storeManager->getStore()->getStoreId();
$product = $objectManager->get('\Magento\Catalog\Model\Product')->load(1);
$finalPriceAmt = $product->getPriceInfo()->getPrice('final_price')->getValue();
echo "Price in base Currency:-".$finalPriceAmt."=>";
$rate = $priceCurrencyObject->convert($finalPriceAmt, $store, 'USD');
echo "Price in other Currency:-".$rate;exit;

Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to get product's price in all allowed currencies?

Thank you for the script @Sunil Patel, but it doesn't work for me.

I have set USD as the base currency for the store and EUR as a default display currency. And I have set USD -> EUR rate:

Screenshot 2017-05-11 13.53.38.png

 

That means all prices are set in USD (in administration):

Screenshot 2017-05-11 13.55.25.png

Then when I go the frontend of the store, I can see the price correctly for both currencies:

Screenshot 2017-05-11 13.56.49.pngScreenshot 2017-05-11 13.56.27.png

 

However, when I run your script, the $finalPriceAmt is 9.89 EUR - which I guess is correct as EUR is the default display currency for the store, but price in USD is $9.89 - like no conversion was done at all.

 

Right now I convert the currency by code:

// $currencyDirectory is instance of \Magento\Directory\Helper\Data
$price = $currencyDirectory->currencyConvert($price, 'EUR', 'USD');

but the price returned by this function is $13.99, not $14 as it should be. Calling currency round method on it doesn't change anything as it keeps the default precision (2 decimals).

 

I tried to go through Magento's scripts (as it correctly display $14 on the frontend), but I didn't manage to find anything useful how can I get to this price.

Re: How to get product's price in all allowed currencies?

And a screenshots how the currency setup looks like for the store:

 

Default config:

Screenshot 2017-05-11 14.08.39.png

 

Store config:

Screenshot 2017-05-11 14.07.10.png

Euro is allowed as well for the store.

 

Any help appreciated as I'm really lost here.

Re: How to get product's price in all allowed currencies?

I managed to move forward a bit with $store->setCurrentCurrencyCode($currencyCode);.

 

Snippet of the code:

$store = $product->getStore();
$store->setCurrentCurrencyCode($currencyCode);

$price = $product->getPriceInfo()->getPrice('final_price')->getValue();
// $price returns correctly 9.89 for EUR and 14 for USD \o/

The only issue is that it doesn't work in a loop. Code:

$store = $product->getStore();
$currencies = ['USD', 'EUR'];
foreach ($currencies as $currencyCode) {
    $store->setCurrentCurrencyCode($currencyCode);

    $price = $product->getPriceInfo()->getPrice('final_price')->getValue();
    // $price is now 14 for both USD and EUR
}

 When I change the order of currencies to ['EUR', 'USD'] I'll get $price = 9.89 for both. It looks like the price is cached somewhere inside the product or priceInfo or priceModel. I wasn't able to find.

 

I tried to always get the new priceInfo object for each iteration by 

$priceInfo = $product->reloadPriceInfo();

but it still returns the same price for both currencies.

 

I tried to do this:

$store->setCurrentCurrencyCode('EUR');
$store->setCurrentCurrencyCode('USD'); // and even vice-versa

$price = $product->getPriceInfo()->getPrice('final_price')->getValue();

to check in which currency I'll get the final price and here it's always the latest set currency (USD in this case). From this I assume the price is cached/stored somewhere inside the object after the first fetch of the price.

 

If you know which object and how to invalidate to get the correct price in each iteration, I'd really appreciate! Thanks a lot!

Re: How to get product's price in all allowed currencies?

@algolia

 

please check below code

 

$store = $product->getStore();
$currencies = ['USD', 'EUR'];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
foreach ($currencies as $currencyCode) {
    $store->setCurrentCurrencyCode($currencyCode);
	$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
    $price = $product->getPriceInfo()->getPrice('final_price')->getValue();
	echo $price;
}



Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer