1

I want to display 2 currencies at a same time on product page, just like $50 and AED200 something like this : https://miatlantic.ae/ws-c3850-24t-s

0

Note : Im not sure if it's a best way.

  1. Stores > Configuration > Currency Setup (USD & AED) enter image description here

  2. Stores > Currency rates

enter image description here

  1. app/design/frontend/{Theme}/{name}/Magento_Catalog/layout/catalog_product_prices.xml

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <block class="Vendor\Modulename\Block\Extracurrency" name="extra-currency-price" template="Magento_Catalog::product/price/amount/extra.phtml" />
        </body>
    </page>
    
  2. copy default.phtml from :

    Vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml

    into :

    app/design/frontend/{Theme}/{name}/Magento_Catalog/templates/product/price/amount/default.phtml

    Then add inside :

    <?php echo $block->getBlockHtml('extra-currency-price'); ?>
    
  3. app/design/frontend/{Theme}/{name}/Magento_Catalog/templates/product/price/amount/extra.phtml

    <div class="additional-price"><?php echo $block->getProductPriceByCurrency('AED') ?></div>
    
  4. You need to add this block in your module

    app/code/{Vendor}/{modulename}/Block/Extracurrency.php

    <?php
    namespace Vendor\Modulename\Block;
    
    class Extracurrency extends \Magento\Framework\View\Element\Template
    {
        protected $_storeManager;
        protected $_objectManager;
        protected $_currency;
        protected $_registry;
    
        public function __construct(
            \Magento\Backend\Block\Template\Context $context,
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\ObjectManagerInterface $objectManager,
            \Magento\Directory\Model\Currency $currency,
            \Magento\Framework\Registry $registry,
            array $data = []
        ) {
            $this->_storeManager = $storeManager;
            $this->_objectManager = $objectManager;
            $this->_currency = $currency;
            $this->_registry = $registry;
            parent::__construct($context, $data);
        }
    
        /**
         * Get product price by currency code
         *
         * @param $code
         * @return mixed
         *
         * @autor @m
         */
        public function getProductPriceByCurrency($code) {
            $currentCurrency = $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
            $currency = $this->_currency->load($currentCurrency);
            $product = $this->_registry->registry('current_product');
            $currencyModelInstance = $this->_objectManager->create('Magento\Directory\Model\Currency');
            $availableCurrency = $this->_storeManager->getStore()->getAvailableCurrencyCodes(); //USD, AED
    
            if (in_array($code, $availableCurrency)) {
                $currencySymbol = $currencyModelInstance->load($code)->getCurrencySymbol();
                $priceConvert = $currency->convert($product->getPrice(), $code);
                $result = $currencyModelInstance->format($priceConvert, ['precision'=> 2, 'symbol' => $currencySymbol], false, false);
            } else {
                $result =  null;
            }
            return $result;  
        }
    }
    
  5. php bin/magento s:d:c
    php bin/magento c:f
    

Result : enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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