Skip to content

fix Display tax percent in current locale number format #32272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ define([
return priceUtils.formatPrice(price, quote.getPriceFormat());
},

/**
* @param {*} amount
* @return {*|String}
*/
getFormattedPercent: function (amount) {
var format = Object.assign({}, quote.getPriceFormat());

format.requiredPrecision = this.countPrecision(amount, format.decimalSymbol);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @engcom-Charlie,
I'm curious about why did you have to calculate the required precision?

I see that the priceUtils.formatPrice() function already has a fallback value (precision of 2 digits) in case format.requiredPrecision is not defined.

Thank you

format.pattern = '%s';

return priceUtils.formatPrice(amount, format, false);
},

/**
* @param {*} amount
* @param {*} decimalSymbol
* @return {*|Number}
*/
countPrecision: function (amount, decimalSymbol) {
var decimalValue = amount.toString().split(decimalSymbol);

return decimalValue[1].length;
},

/**
* @return {*}
*/
Expand Down
83 changes: 46 additions & 37 deletions app/code/Magento/Tax/view/frontend/templates/order/tax.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,64 @@
* See COPYING.txt for license details.
*/

use Magento\Framework\Currency;
use Magento\Framework\Escaper;
use Magento\Tax\Block\Sales\Order\Tax;
use Magento\Tax\Helper\Data;

/** @var Escaper $escaper */
/** @var Tax $block */

// phpcs:disable Magento2.Templates.ThisInTemplate
// phpcs:disable Squiz.PHP.GlobalKeyword.NotAllowed
?>
<?php
$_order = $block->getOrder();
$_source = $block->getSource();
$_fullInfo = $this->helper(\Magento\Tax\Helper\Data::class)->getCalculatedTaxes($_source);
$order = $block->getOrder();
$source = $block->getSource();
$fullInfo = $this->helper(Data::class)->getCalculatedTaxes($source);
global $taxIter;
$taxIter++;
?>

<?php if ($_fullInfo && $block->displayFullSummary()) : ?>
<?php foreach ($_fullInfo as $info) : ?>
<?php
$percent = $info['percent'];
$amount = $info['tax_amount'];
$baseAmount = $info['base_tax_amount'];
$title = $info['title'];
?>
<tr class="totals tax details details-<?= (int) $taxIter ?><?= ($block->getIsPlaneMode()) ? ' plane' : '' ?>">
<td <?= /* @noEscape */ $block->getLabelProperties() ?>>
<?= $block->escapeHtml($title) ?>
<?php if ($percent !== null) : ?>
(<?= (float) $percent ?>%)
<?php endif; ?>
<br />
</td>
<td <?= /* @noEscape */ $block->getValueProperties() ?> rowspan="1">
<?= /* @noEscape */ $_order->formatPrice($amount) ?>
</td>
</tr>
<?php endforeach; ?>
<?php if ($fullInfo && $block->displayFullSummary()): ?>
<?php foreach ($fullInfo as $info) : ?>
<?php
$percent = $info['percent'];
$amount = $info['tax_amount'];
$baseAmount = $info['base_tax_amount'];
$title = $info['title'];
?>
<tr class="totals tax details details-<?= (int)$taxIter . $block->getIsPlaneMode() ? ' plane' : '' ?>">
<td <?= /* @noEscape */ $block->getLabelProperties() ?>>
<?= $escaper->escapeHtml($title) ?>
<?php if ($percent !== null): ?>
<?php $percent = (float)$percent; ?>
<?= $order->getOrderCurrency()->formatTxt($percent, ['display' => Currency::NO_SYMBOL]) . '%' ?>
<?php endif; ?>
<br/>
</td>
<td <?= /* @noEscape */ $block->getValueProperties() ?> rowspan="1">
<?= /* @noEscape */ $order->formatPrice($amount) ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>

<?php if ($block->displayFullSummary() && $_fullInfo && !$block->getIsPlaneMode()) : ?>
<tr class="totals-tax-summary">
<?php elseif ($block->displayFullSummary() && $_fullInfo && $block->getIsPlaneMode()) : ?>
<tr class="totals-tax-summary plane">
<?php else : ?>
<tr class="totals-tax">
<?php if ($block->displayFullSummary() && $fullInfo && !$block->getIsPlaneMode()): ?>
<tr class="totals-tax-summary">
<?php elseif ($block->displayFullSummary() && $fullInfo && $block->getIsPlaneMode()): ?>
<tr class="totals-tax-summary plane">
<?php else: ?>
<tr class="totals-tax">
<?php endif; ?>
<th <?= /* @noEscape */ $block->getLabelProperties() ?> scope="row">
<?php if ($block->displayFullSummary()) : ?>
<div class="detailed"><?= $block->escapeHtml(__('Tax')) ?></div>
<?php else : ?>
<?= $block->escapeHtml(__('Tax')) ?>
<th <?= /* @noEscape */$block->getLabelProperties() ?> scope="row">
<?php if ($block->displayFullSummary()): ?>
<div class="detailed"><?= $escaper->escapeHtml(__('Tax')) ?></div>
<?php else: ?>
<?= $escaper->escapeHtml(__('Tax')) ?>
<?php endif; ?>
</th>
<td <?= /* @noEscape */ $block->getValueProperties() ?> data-th="<?= $block->escapeHtmlAttr(__('Tax')) ?>">
<?= /* @noEscape */ $_order->formatPrice($_source->getTaxAmount()) ?>
<td <?= /* @noEscape */ $block->getValueProperties() ?> data-th="<?= $escaper->escapeHtmlAttr(__('Tax')) ?>">
<?= /* @noEscape */ $order->formatPrice($source->getTaxAmount()) ?>
</td>
</tr>
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ define([
return this.getFormattedPrice(amount);
},

/**
* @param {*} amount
* @return {*|String}
*/
formatPercent: function (amount) {
return this.getFormattedPercent(amount);
},

/**
* @param {*} parent
* @param {*} percentage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<!-- ko foreach: rates -->
<tr class="totals-tax-details">
<!-- ko if: percent -->
<th class="mark" scope="row" data-bind="text: title + ' (' + percent + '%)'"></th>
<th class="mark" scope="row" data-bind="text: title + ' (' + $parents[1].formatPercent(percent) + '%)'"></th>
<!-- /ko -->
<!-- ko if: !percent -->
<th class="mark" scope="row" data-bind="text: title"></th>
Expand Down