I want to show currency symbol and amount in separate html elements such as: BD18.00
into this:
<span>BD</span><span>18.00</span>
is it achievable?
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currencyCode = $storeManager->getStore()->getCurrentCurrencyCode();
$currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode);
echo "<span>{$currency->getCurrencySymbol()}</span><span>{$product->getPrice()}</span>";
?>
You can change the price format by change theme files.
\app\design\frontend\vendor\theme\Magento_Catalog\templates\product\price\amount\default.phtml
Just replace or comment
<?= /* @escapeNotVerified */ $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>
and add the following snippet
<span class="av-new-price"><?= $block->getDisplayCurrencyCode() ?></span>
<span class="av-amount"><?php echo number_format($block->getDisplayValue(),2) ?></span>
If this file not in your directory just overrides.
You can use below code to get the currency symbol and currency code in your template file:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface')->getStore();
$currencyCode = $storeManager->getCurrentCurrencyCode();
$currencyFactory = $objectManager->create('Magento\Directory\Model\CurrencyFactory');
$currency = $currencyFactory->create()->load($currencyCode);
$currencySymbol = $currency->getCurrencySymbol();
$productPrice = $_product->getPrice();
$productFinalPrice = $_product->getFinalPrice();
echo "<span class='cur-code'>".$currencyCode."</span>"; // prints currency code of your current store. E.g. 'USD' OR 'EUR' OR 'GBP' OR 'INR' etc..
echo "<span class='cur-symbol'>".$currencySymbol."</span>"; // prints currency symbol of your current store. E.g. '$' OR '€' OR '£' OR '₹' etc..
echo "<span class='prod-price'>".$productPrice."</span>";// prints product's price value.
echo "<span class='final-price'>".$productFinalPrice ."</span>";// prints value of product's final price.
?>
This is a tested code and will print the values as per your current store or as per the active currency switcher.
Hope this will help you! :)
try this below code
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currencysymbol = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currency = $currencysymbol->getStore()->getCurrentCurrency();