Question

I will trying but its not working ?

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
    $currencyModel = $objectManager->create('Magento\Directory\Model\Currency'); // Instance of Currency Model
    $price =$block->getProductPrice($_product);

    $currencyCode = 'SAR';
    $currencySymbol = $currencyModel->load($currencyCode)->getCurrencySymbol();
    $precision = 2;   
    echo $formattedPrice = $currencyModel->format($price, ['symbol' => $currencySymbol, 'precision'=> $precision], true, false);
    var_dump($formattedPrice);
?>

How to solve this problams?

Était-ce utile?

La solution 2

     <?php   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
         $currencysymbol = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
         echo "<br>";

         $currencysymbol = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
         $currency2 = $currencysymbol->getStore()->getCurrentCurrencyCode();

         if ($currency2=="AED") {

         $currency = $objectManager->create('Magento\Directory\Model\Currency')->load('AED'); 
         $currency2 = $currencysymbol->getStore()->getCurrentCurrencyCode();

        echo  "<h4 class=product-price>AED".number_format($currency->convert($product->getPrice(),'AED'), 2)."</h4>";
         }
         if ($currency2=="SAR") {

            $currency = $objectManager->create('Magento\Directory\Model\Currency')->load('AED'); 
            $currency2 = $currencysymbol->getStore()->getCurrentCurrencyCode();

         echo  "<h4 class=product-price>SAR".number_format($currency->convert($product->getPrice(),'SAR'), 2)."</h4>";
            } ?>

Its Working ....

Autres conseils

You should add the option 'display' => \Magento\Framework\Currency::USE_SYMBOL in order to get the symbol.

Change your format() call like this:

echo $formattedPrice = $currencyModel->format(
         $price, [
                   'symbol' => $currencySymbol, 
                   'precision'=> $precision, 
                   'display' => \Magento\Framework\Currency::USE_SYMBOL], 
         true, false);
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top