Adding custom text after order total in Woocommerce orders and emails

拟墨画扇 提交于 2021-01-28 07:55:41

问题


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;
}

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?


回答1:


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;
}

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;
}

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



来源:https://stackoverflow.com/questions/65581987/adding-custom-text-after-order-total-in-woocommerce-orders-and-emails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!