自定义 WooCommerce 结帐页面中的文本 "Total"

Customize the text "Total" in WooCommerce checkout page

我想将结帐页面中的 "Total" 文本更改为 "Total inkl. vat"。我试过不同的东西都没有成功......

这是我的目标:

<?php _e( 'Total', 'woocommerce' ); ?>

这是代码片段。我搜索了所有语言文件,但找不到任何内容。我已经安装了 Q 翻译插件,但我认为这不是问题所在。

我可以努力编写代码,但这不是一个好的解决方案,因为我必须在我的所有文件中进行此编辑。

请问我该如何实现?

谢谢

选项 1 (最佳选项)

Overriding the woocommerce checkout/review-order.php 模板。

You need first (if not done) to copy the templates sub folder located in in woocommerce plugin folder to your active child theme (or theme) folde, and rename it woocommerce.

完成活动主题后,转到 woocommerce > checkout,然后 open/edit review-order.php 模板文件。

在这个模板的最后你有这个:

        <?php do_action( 'woocommerce_review_order_before_order_total' ); ?>

        <tr class="order-total">
            <th><?php _e( 'Total', 'woocommerce' ); ?></th>
            <td><?php wc_cart_totals_order_total_html(); ?></td>
        </tr>

        <?php do_action( 'woocommerce_review_order_after_order_total' ); ?>

    </tfoot>
</table>

所以你会改变:

<th><?php _e( 'Total', 'woocommerce' ); ?></th>

收件人:

<th><?php _e( 'Total inkl. vat', 'woocommerce' ); ?></th>

现在可以保存了,大功告成……

参考文献:


选项 2 (不理想,见下文)

您可以为此目的使用 wordpress gettex() 原生函数,这样:

add_filter('gettext', 'wc_renaming_checkout_total', 20, 3);
function wc_renaming_checkout_total( $translated_text, $untranslated_text, $domain ) {

    if( !is_admin() && is_checkout ) {
        if( $untranslated_text == 'Total' )
            $translated_text = __( 'Total inkl. vat','theme_slug_domain' );
    }
    return $translated_text;
}

此代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

But you will get 2 customized texts in the prices table, because there is 2 "Total" texts (once in the first line after 'Products') and one time at the end…

此代码已经过测试并且有效。