如何在 WooCommerce 中显示带有货币详细信息的金额的拼写

How display the spell out of an amount with currency details in WooCommerce

我正在尝试拼出 WooCommerce 订单总金额,在我的 invoice.php 模板文件中可以达到订单总金额。

首先我尝试了:

$total = $order->get_total();
<?php echo ( $total ); ?> - 

<?php 
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $f->format($total); ?>  

订单总计显示为 225.00 拼写显示为:225


编辑:

我找到了以下解决方案:

<?php  $number = $order->get_total() ;

$formatter = new NumberFormatter('tr', NumberFormatter::SPELLOUT);

$formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");

echo $formatter->format($number); ?>

但是结果是这样的:225

所需的显示应为:225 土耳其里拉,零便士

我该怎么做?

NumberFormatter SPELLOUT 常量不处理小数。

您可以使用以下自定义函数来显示拼写为 (带有货币详细信息):

的浮点数金额
function wc_spellout_amount( $amount, $country_code = 'tr' ) {
    $formatter = new NumberFormatter($country_code, NumberFormatter::SPELLOUT);
    $formatter->setTextAttribute(NumberFormatter::DEFAULT_RULESET, "%financial");

    $amounts = explode('.', (string) $amount); // Separating decimals from amount

    $output  = $formatter->format($amounts[0]);
    $output .= ' ' . _n('turkish lira', 'turkish liras', $amounts[0], 'woocommerce');
    $output .= ', ' . $formatter->format($amounts[1]);
    $output .= ' ' . _n('penny', 'pennies', ( $amounts[1] > 0 ? $amounts[1] : 1 ), 'woocommerce');

    return $output;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。

用法: 然后你将在你的代码中按如下方式使用它:

echo wc_spellout_amount( $order->get_total() );

已测试并有效。