【问题标题】:Adding custom text after order total in Woocommerce orders and emails在 Woocommerce 订单和电子邮件中的订单总额后添加自定义文本
【发布时间】:2021-04-11 09:15:42
【问题描述】:

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

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,但我似乎无法使用这个过滤器。如何修改上面的函数以在普通订单电子邮件中的价格之后显示自定义文本?

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

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

    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 文件中。经过测试并且可以工作。


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

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 只是为了澄清:第一个功能将在“谢谢您/已收到订单”页面和电子邮件中显示文本,但不会在购物车结账时显示文本。第二个只会在电子邮件中显示。而我原来的功能只会在购物车结账页面上显示?
    猜你喜欢
    • 2016-03-03
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2022-01-10
    • 1970-01-01
    相关资源
    最近更新 更多