2

Is it possible to get the price without currency symbol, or a way to put a div around the currency symbol to hide it with css?

$block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer())

If I remove the $block->formatCurrency I get the price without currency symbol, but a 1 will be added at the end of the price.

So I would like to put a div around the currency symbol and hide it with CSS.

Someone who can help me with that?

EDIT: I also want to add ,- behind the price.

4 Answers 4

0

Yes it is possible to get price without currency symbol by using below approach

Instantiate \Magento\Directory\Model\Currency $priceCurrencyFormat class in constructor and use below code

$this->priceCurrencyFormat->format($product->getPrice(), ['precision'=>0, 'display'=>\Zend_Currency::NO_SYMBOL], 0);

In second param of format() func you can also customize the price precision based on your requirement.

You could also use below code to get your result, but it is not a good approach

$objectManager    = \Magento\Framework\App\ObjectManager::getInstance();
$priceCurrencyFormat = $objectManager->get('Magento\Directory\Model\Currency');
$priceFormat = $priceCurrencyFormat->format($product->getPrice(), ['precision'=>0, 'display'=>\Zend_Currency::NO_SYMBOL], 0);
echo $priceFormat."-";
3
  • Hi, I can't find the file to edit. Are you sure is the correct path for Magento 2?
    – n00bly
    May 8, 2019 at 10:29
  • Directly editing the core file is not recommended, you should override the file in which you need your changes. In that overdid file (say you overdid any block file ) you can create the function with the above code and call it in your file.
    – Prabhu M.
    May 8, 2019 at 10:54
  • I have updated my answer, placing those 4 lines of code in your desired files will give you your desired result.
    – Prabhu M.
    May 8, 2019 at 11:13
0

May be this will help you

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$currency = $objectManager->get('Magento\Directory\Model\Currency');
$price = 100;
echo $currency->format($price, ['display'=>\Zend_Currency::NO_SYMBOL], false);
?>
0

You guys can use this extension.

https://marketplace.magento.com/extrembler-overridecurrencysymbol.html

Which is paid.

Thanks

0

I've just faced a similar task and found this solutions:

  1. For home page and category pages.

You need to override app\design\frontend\Vendor\theme\Magento_Catalog\templates\product\price\amount\default.phtml.

In my case it was 20 line. The line looked like this:

><?= $block->escapeHtml($block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()), ['span']) ?></span>

And now it was changed to this:

><span class="price"><?= $block->escapeHtml($block->getDisplayValue()) ?></span></span>
  1. For product page.

You need to create require-config.js in app\design\frontend\Vendor\theme with content:

var config = {
map: {
    '*': {
        priceBox:'Magento_Catalog/js/custom-price-box',
    }
};

Then create your custom price box in app\design\frontend\Vendor\theme\Magento_Catalog\web\js\custom-price-box.js. The content is:

define(
[
    'jquery',
    'Magento_Catalog/js/price-utils',
    'underscore',
    'mage/template',
    'Magento_Catalog/js/price-box',
    'jquery/ui'
],
function ($, utils, _, mageTemplate) {

    'use strict';

    $.widget('customName.priceBox', $.mage.priceBox, {
        /*eslint-disable no-extra-parens*/
        /**
         * Render price unit block.
         */
        reloadPrice: function reDrawPrices() {
            var priceFormat = (this.options.priceConfig && this.options.priceConfig.priceFormat) || {},
                priceTemplate = mageTemplate(this.options.priceTemplate);

            _.each(this.cache.displayPrices, function (price, priceCode) {
                price.final = _.reduce(price.adjustments, function (memo, amount) {
                    return memo + amount;
                }, price.amount);

                price.formatted = price.final + ',-'; //you need to change this line, as example

                $('[data-price-type="' + priceCode + '"]', this.element).html(priceTemplate({
                    data: price
                }));
            }, this);
        },
    });

    return $.stagem.priceBox;
});

And my price now showing without currency symbol. Hope this helps.

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.