1

I have 2 websites in my magento. I have created one custom extension. In custom extension, i have form to fill which have firstname , last name, product price, store id.

Website 1 currency = USD (Deafult currency) Website 2 currency = EUR

When i submit form data will be saved in custom table and shown in custom admin grid. I want to show price according to the store wise entry.

If i fill form website 1 then my price needs to show like $54.00 and if i fill form from website 2 then price needs to show like €10.00.

I have used below code to show formatted price with currency in admin.

<?php
 
namespace Vendor\Extension\Block;
 
 
class Customprice extends \Magento\Framework\View\Element\Template
{
 
    protected $priceCurrency; 
 
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        array $data = []
    ) {
        $this->priceCurrency = $priceCurrency;
        parent::__construct($context, $data);
    }
 
    public function getCurrencyWithFormat($price)
    {
        return $this->priceCurrency->format($price,true,2);
    }
}

In website 2 if $price = 10; then according to above code it will be shown like $10.00 but in website 2 it should show like €10.00.

$formmattedPrice = $this->getCurrencyWithFormat($price);

I am facing problem only in admin. In front end it shows proper currency according to store wise.

1 Answer 1

0

You can try below way to formatted price according to store wise with proper currency symbol.

Please pass your store id in below helper function to get proper formmatted price.

<?php
namespace PackageName\Module\Helper;

use Magento\Directory\Model\Currency;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\Pricing\Helper\Data
     */
    protected $priceHelper;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var \Magento\Directory\Model\Currency
     */
    protected $currency;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        Currency $currency = null
    ) {
        parent::__construct($context);
        $this->priceHelper = $priceHelper;
        $this->_storeManager = $storeManager;
        $this->currency = $currency ?: \Magento\Framework\App\ObjectManager::getInstance()
            ->create(Currency::class);
    }

    /**
     * get currency code
     *
     * @param int $storeId
     * @return mixed 
     */
    public function getCurrentCurrencyCode($storeId)
    {
        return $this->_storeManager->getStore($storeId)->getCurrentCurrencyCode();
    }  

    /*Get price format */
    public function getFormattedPrice($storeId, $price)
    {
        $currencyCode = $this->getCurrentCurrencyCode($storeId);
        $purchaseCurrency = $this->currency->load($currencyCode);
        return $purchaseCurrency->format($price, [], false);
    }
}

Please use above function getFormattedPrice($storeId, $price) and pass store id , price in this function to get store wise currency in admin.

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.