Read More
Read More
Menu Close

    Get formatted price by currency code in magento2

    Get formatted price by currency code in magento2 – We all know that magento catalog product model provides method to get formatted product price but it gives price in current store currency, so if having issue in getting formatted price in magento2 in a desired currency then no need to worry about this. Here I am going to explain how to get formatted price in desired currency rate.

    It is very to get formatted price just only need to follow few easy steps and you will get the desired out. So here following the given steps –

    Step 1 : Create construct method .

    /**
     * @var \Magento\Framework\Pricing\PriceCurrencyInterface
     */
    protected $_priceCurrency;
    
    /**
     * @param Context                                            $context
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface  $priceCurrency
     * @param array                                              $data
     */
    public function __construct(
        Context $context,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        array $data = []
    ) {
        $this->_priceCurrency = $priceCurrency;
        parent::__construct($context, $data);
    }

    Step 2 : Create formatted price method to get formatted price by currency code.

    /**
     * Get formatted by price and currency
     *
     * @param   $price
     * @param   $currency
     * @return  array || float
     */
    public function getFormatedPrice($price, $currency)
    {
        $precision = 2,   // for displaying price decimals 2 point
        return $this->_priceCurrency->format(
            $price,
            $includeContainer = true,
            $precision,
            $scope = null,
            $currency
        );
    }

    So If you call getFormatedPrice(100, ‘USD’) then it will return $100.00 value, so in this way you can get formatted price in desired currency.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top