在 Woocommerce 订单和电子邮件中的订单总额后添加自定义文本 [英] Adding custom text after order total in Woocommerce orders and emails

查看:53
本文介绍了在 Woocommerce 订单和电子邮件中的订单总额后添加自定义文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用它在购物车和结帐页面上为来自特定国家/地区的客户显示自定义文本:

I am using this to display a custom text for customers from specific countries on the cart and checkout page:

add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_total_message_html', 10, 1 );

function custom_total_message_html( $value ) {
if( in_array( WC()->customer->get_shipping_country(), array('US', 'CA') ) ) {
    $value .= '<small>' . __('My text.', 'woocommerce') . '</small>';
}
return $value;
}

然而,这不会在 Woocommerce 发送的普通订单电子邮件中的订单总数之后添加自定义文本.我知道有一个过滤器 woocommerce_get_formatted_order_total 但我似乎无法使用此功能.如何修改我上面的函数以在普通订单电子邮件中显示价格后的自定义文本?

This does however NOT add the custom text after the order totals in the plain order emails that Woocommerce is sending. I know there is a filter woocommerce_get_formatted_order_total but I cannot seem to get a working function with this. How can I modify my function above to also display the custom text after the price in the plain order emails?

推荐答案

要在 WooCommerce 订单和电子邮件中显示此自定义文本,请使用以下内容:

For displaying this custom text in WooCommerce orders and emails after total, use the following:

add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_message_html', 10, 3 );
function custom_order_total_message_html( $total_rows, $order, $tax_display ) {
    if( in_array( $order->get_shipping_country(), array('US', 'CA') ) && isset($total_rows['order_total']) ) {
        $total_rows['order_total']['value'] .= ' <small>' . __('My text.', 'woocommerce') . '</small>';
    }
    return $total_rows;
}

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

要使其仅适用于电子邮件通知,请使用:

To make it work only on email notifications use:

add_filter( 'woocommerce_get_order_item_totals', 'custom_order_total_message_html', 10, 3 );
function custom_order_total_message_html( $total_rows, $order, $tax_display ) {
    if( in_array( $order->get_shipping_country(), array('US', 'CA') ) && isset($total_rows['order_total']) && ! is_wc_endpoint_url() ) {
        $total_rows['order_total']['value'] .= ' <small>' . __('My text.', 'woocommerce') . '</small>';
    }
    return $total_rows;
}

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于在 Woocommerce 订单和电子邮件中的订单总额后添加自定义文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆