Search code examples
woocommercewoocommerce-theming

Add order ID to customer sender name in WooCommerce email notification


How can I add an order number (order_id) to E-Mail Sender Name as follows: #order_id John Doe

For Example: "My Shop" should be changed to "#1234 John Doe".

I am using Change sender name to customer billing full name in WooCommerce email notifications answer code.

Can anyone help me?


Solution

  • Use the get_id() function to retrieve the order number and use sprintf() to give your string the desired format, like this:

    add_filter( 'woocommerce_email_from_name', 'filter_wc_email_from_name', 10, 2 );
    function filter_wc_email_from_name( $from_name, $email ) {
        if ( $email->id == 'new_order' && is_a($email->object, 'WC_Order') ) {
            $order     = $email->object;
            $from_name = sprintf( '#%s %s', $order->get_id(), $order->get_formatted_billing_full_name() );
        }
        return $from_name;
    }