0

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.

8
  • It is already same price 42.00. What you want to do ? Commented Jul 14, 2017 at 5:59
  • I have updated question. please check it. Commented Jul 14, 2017 at 5:59
  • What is the actual price of the product ? Commented Jul 14, 2017 at 6:02
  • Let say actual price of product is 100$ but if I will switch currency to AED then it will become 367.28 AED. So in this case instead of 367.28 AED I want t display like 368 AED in mini cart only. So if I will get the file from where it is coming there I can round price. Commented Jul 14, 2017 at 6:05
  • Okay. You can use checkout_cart_product_add_after observer. And in observer execute method, you can write logic to round product price using ceil or floor so that it will be as per your requirement. Commented Jul 14, 2017 at 6:08

2 Answers 2

1

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

3
  • Thansk for your answer let me try this. Commented Jul 17, 2017 at 5:14
  • ok thanks for your reply and get back with u r comments Commented Jul 17, 2017 at 6:08
  • Actually I want to format price for mini cart item only Commented Jul 17, 2017 at 9:56
0

1.Create di.xml in Vendor/Module/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
   <preference for="Magento\Checkout\CustomerData\DefaultItem" type="Vendor\Module\CustomerData\DefaultItem" />
</config>

2.Create file DefaultItem.php inside Vendor\Module\CustomerData and do what you want in price values here they will reflect on mini cart.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\CustomerData;

use Magento\Framework\App\ObjectManager;

/**
 * Default item
 */
class DefaultItem extends \Magento\Checkout\CustomerData\DefaultItem
{
         /**
         * @var \Magento\Catalog\Helper\Image
         */
        protected $imageHelper;

        /**
         * @var \Magento\Msrp\Helper\Data
         */
        protected $msrpHelper;

        /**
         * @var \Magento\Framework\UrlInterface
         */
        protected $urlBuilder;

        /**
         * @var \Magento\Catalog\Helper\Product\ConfigurationPool
         */
        protected $configurationPool;

        /**
         * @var \Magento\Checkout\Helper\Data
         */
        protected $checkoutHelper;

        /**
         * Escaper
         *
         * @var \Magento\Framework\Escaper
         */
        private $escaper;
    public function __construct(
        \Magento\Catalog\Helper\Image $imageHelper,
        \Magento\Msrp\Helper\Data $msrpHelper,
        \Magento\Framework\UrlInterface $urlBuilder,
        \Magento\Catalog\Helper\Product\ConfigurationPool $configurationPool,
        \Magento\Checkout\Helper\Data $checkoutHelper,
        \Magento\Framework\Escaper $escaper = null
    ) {
        $this->configurationPool = $configurationPool;
        $this->imageHelper = $imageHelper;
        $this->msrpHelper = $msrpHelper;
        $this->urlBuilder = $urlBuilder;
        $this->checkoutHelper = $checkoutHelper;
        $this->escaper = $escaper ?: ObjectManager::getInstance()->get(\Magento\Framework\Escaper::class);
    }

    protected function doGetItemData()
    {
        $imageHelper = $this->imageHelper->init($this->getProductForThumbnail(), 'mini_cart_product_thumbnail');
        $productName = $this->escaper->escapeHtml($this->item->getProduct()->getName());

        return [
            'options' => $this->getOptionList(),
            'qty' => $this->item->getQty() * 1,
            'item_id' => $this->item->getId(),
            'configure_url' => $this->getConfigureUrl(),
            'is_visible_in_site_visibility' => $this->item->getProduct()->isVisibleInSiteVisibility(),
            'product_id' => $this->item->getProduct()->getId(),
            'product_name' => $productName,
            'product_sku' => $this->item->getProduct()->getSku(),
            'product_url' => $this->getProductUrl(),
            'product_has_url' => $this->hasProductUrl(),
            'product_price' => $this->checkoutHelper->formatPrice($this->item->getCalculationPrice()),
            'product_price_value' => $this->item->getCalculationPrice(),
            'weight' => $this->item->getProduct()->getWeight(),
            'product_image' => [
                'src' => $imageHelper->getUrl(),
                'alt' => $imageHelper->getLabel(),
                'width' => $imageHelper->getWidth(),
                'height' => $imageHelper->getHeight(),
            ],
            'canApplyMsrp' => $this->msrpHelper->isShowBeforeOrderConfirm($this->item->getProduct())
                && $this->msrpHelper->isMinimalPriceLessMsrp($this->item->getProduct()),
        ];
    }

}
?>
1
  • Possibly this worked in a previous version of M2 but not as of 2.4 at least, probably earlier. This function is not called when creating the minicart. Commented May 24, 2022 at 8:58

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.