1

Product Detail, how to add space after currency in magento 2?

Below is code

app/design/frontend/Custom/goshop/Magento_Catalog/templates/product/price/amount/default.phtml

<?php /** @var \Magento\Framework\Pricing\Render\Amount $block */ ?>

<span class="price-container <?php /* @escapeNotVerified */ echo $block->getAdjustmentCssClasses() ?>"
        <?php echo $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
    <?php if ($block->getDisplayLabel()): ?>
        <span class="price-label"><?php /* @escapeNotVerified */ echo $block->getDisplayLabel(); ?></span>
    <?php endif; ?>
    <span <?php if ($block->getPriceId()): ?> id="<?php echo $id ?><?php /* @escapeNotVerified */ echo $block->getPriceId() ?>"<?php endif;?>
        <?php echo($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
        data-price-amount="<?php /* @escapeNotVerified */ echo $block->getDisplayValue(); ?>"
        data-price-type="<?php /* @escapeNotVerified */ echo $block->getPriceType(); ?>"
        class="price-wrapper <?php /* @escapeNotVerified */ echo $block->getPriceWrapperCss(); ?>"
        <?php echo $block->getSchema() ? ' itemprop="price"' : '' ?>>
        <?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>
    </span>
    <?php if ($block->hasAdjustmentsHtml()): ?>
        <?php echo $block->getAdjustmentsHtml() ?>
    <?php endif; ?>
    <?php if ($block->getSchema()): ?>
        <meta itemprop="priceCurrency" content="<?php /* @escapeNotVerified */ echo $block->getDisplayCurrencyCode()?>" />
    <?php endif; ?>
</span>

enter image description here

1
  • You can explode string $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) with RM Jan 8, 2018 at 15:57

4 Answers 4

0

You can achieve like this:

    <?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
// @codingStandardsIgnoreFile
$id = rand() . time();
?>

<?php
/** @var \Magento\Framework\Pricing\Render\Amount $block */
$priceproduct = $block->formatCurrency($block->getDisplayValue(), (bool) $block->getIncludeContainer());
if (strpos($priceproduct, 'RM') !== false) {
    $pricearr = explode("RM", $priceproduct);
    $pricespace = $pricearr[0] . "RM " . $pricearr[1];
} else {
    $pricespace = $priceproduct;
}
?>

<span class="price-container <?php /* @escapeNotVerified */ echo $block->getAdjustmentCssClasses() ?>"
      <?php echo $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
          <?php if ($block->getDisplayLabel()): ?>
        <span class="price-label"><?php /* @escapeNotVerified */ echo $block->getDisplayLabel(); ?></span>
    <?php endif; ?>
    <span <?php if ($block->getPriceId()): ?> id="<?php echo $id ?><?php /* @escapeNotVerified */ echo $block->getPriceId() ?>"<?php endif; ?>
    <?php echo($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
                                              data-price-amount="<?php /* @escapeNotVerified */ echo $block->getDisplayValue(); ?>"
                                              data-price-type="<?php /* @escapeNotVerified */ echo $block->getPriceType(); ?>"
                                              class="price-wrapper <?php /* @escapeNotVerified */ echo $block->getPriceWrapperCss(); ?>"
                                              <?php echo $block->getSchema() ? ' itemprop="price"' : '' ?>>
                                              <?php /* @escapeNotVerified */ echo $pricespace; ?>
    </span>
    <?php if ($block->hasAdjustmentsHtml()): ?>
        <?php echo $block->getAdjustmentsHtml() ?>
    <?php endif; ?>
    <?php if ($block->getSchema()): ?>
        <meta itemprop="priceCurrency" content="<?php /* @escapeNotVerified */ echo $block->getDisplayCurrencyCode() ?>" />
    <?php endif; ?>
</span>
7

enter image description here

Just do like above screenshot in Magento 2 admin and space will be shown in frontend.

3
  • What if we used trim()method while displaying currency symbol
    – user60445
    Feb 1, 2018 at 15:53
  • we can achieve a thing in different ways. For non programming people, my answer is good. For coders, yours. :) Feb 2, 2018 at 5:44
  • I did this from admin panel.It working for German language but for english it adds 2 spaces.It already add &nbsp; for english but not for German. How to get resolve this. Feb 14, 2020 at 6:00
2

If you want to add space after symbol in whole website you can do this by just changing from admin panel. Go to Stores -> Currency Symbols and add space after your currency.

0

I don't Know if is the right way, but I did that removing:

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

and adding:

<span class="money-symbol">
  <?php echo $block->getDisplayCurrencySymbol(); ?>
</span>
<span>
  <?php echo $block->getDisplayValue(); ?>
</span>

This way you can control space with css margin or padding applied to class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.