Magento 2 Mini Cart – Item Price Change

magento2mini-cartprice

I am facing rounding issue in mini cart item price. I want to round price for mini cart item.But I am not sure from where it is coming? Please let me know where can I modify cart item price value.
Any help will be appreciated

enter image description here
I just need to change format. I don't want to change price. In my case price is coming like 100.01$ but instead of that I want to show as 100$ only.

Thanks in advance.

Best Answer

Please use the below Observer currency_display_options_forming from your custom module Here Ewall_Account is my custom module.

Step 1: create events.xml file and keep the code in the below path as shown

app/code/Ewall/Account/etc/frontend/events.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="currency_display_options_forming">
    <observer name="Ewall_Account_localize_currencysymbol_currency_display_options" instance="Ewall\Account\Observer\ModifyCurrencyOptions" />
    </event>
    </config>

Step 2: app/code/Ewall/Account/Observer/ModifyCurrencyOptions.php

<?php

namespace Ewall\Account\Observer;

use Magento\Framework\Locale\Currency;
use Magento\Framework\Event\ObserverInterface;

class ModifyCurrencyOptions implements ObserverInterface
{
    /**
     * @var \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory
     */
    protected $symbolFactory;

    const RIGHT = 16;

    /**
     * @param \Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory
     */
    public function __construct(\Magento\CurrencySymbol\Model\System\CurrencysymbolFactory $symbolFactory)
    {
        $this->symbolFactory = $symbolFactory;
    }

    /**
     * Generate options for currency displaying with custom currency symbol
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/stackexchange.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $baseCode = $observer->getEvent()->getBaseCode();
        $logger->info("xxxxxxxxxxxxxxxxxx===>".$baseCode);
        $currencyOptions = $observer->getEvent()->getCurrencyOptions();
        $originalOptions = $currencyOptions->getData();
        $currencyOptions->setData($this->getCurrencyOptions($baseCode, $originalOptions));

        return $this;
    }

    /**
     * Get currency display options
     *
     * @param string $baseCode
     * @param array $originalOptions
     * @return array
     */
    protected function getCurrencyOptions($baseCode, $originalOptions)
    {
        $currencyOptions = [];

        if ($baseCode == 'USD') {

            $currencyOptions['position'] = self::RIGHT; // switch currency symbol position to the RIGHT
        }

        return array_merge($originalOptions, $currencyOptions);
    }
}

Step 3: Please use the below commands

php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Step 4: Please find the output below

please see the output