fakasmile1
fakasmile1

Reputation: 69

Display a custom text based on payment methods in Woocommerce orders

In Woocommerce I'm trying to display a message based on the payment method that the customer selects when an order is submitted. I have 2 payment methods, BACS and Cheque and I need to display a different message for each one.

I just found that you can put a message on the page of thankyou.php

but I would need this custom message to appear on the order page and also to be added to the pdf invoice (I am using WooCommerce PDF Invoices plugin).

Upvotes: 0

Views: 4945

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254362

The following will first save a custom message based on payment gateways as custom order meta data (custom-fields)… This will allow you to set this order custom field in your PDF invoice with much more ease (see the note at the end):

// Save payment message as order item meta data
add_filter( 'woocommerce_checkout_create_order', 'save_custom_message_based_on_payment', 10, 2 );
function save_custom_message_based_on_payment( $order, $data ){
    if ( $payment_method = $order->get_payment_method() ) {
        if ( $payment_method === 'cheque' ) {
            // For Cheque
            $message = __("My custom message for Cheque payment", "woocommerce");
        } elseif ( $payment_method === 'bacs' ) {
            // Bank wire
            $message = __("My custom message for Bank wire payment", "woocommerce");
        }
        // save message as custom order meta data (custom field value)
        if ( isset($message) )
            $order->update_meta_data( '_payment_message', $message );
    }
}

Then the following will display this custom message on Order received page, view order page and email notifications, using hooks (not changing templates):

// On "Order received" page (add payment message)
add_filter( 'woocommerce_thankyou_order_received_text', 'thankyou_custom_payment_message', 10, 2 );
function thankyou_custom_payment_message( $text, $order ) {
    if ( ! is_a($order, 'WC_Order') ) return; 
    if ( $message = $order->get_meta( '_payment_message' ) ) {
        $text .= '<br><div class="payment-message"><p>' . $message . '</p></div>' ;
    }
    return $text;
}

// On "Order view" page (add payment message)
add_action( 'woocommerce_view_order', 'view_order_custom_payment_message', 5, 1 );
function view_order_custom_payment_message( $order_id ){
    if ( $message = get_post_meta( $order_id, '_payment_message', true ) ) {
        echo '<div class="payment-message"><p>' . $message . '</p></div>' ;
    }
}

// Email notifications display (optional)
add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 10, 4 );
function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    if( $sent_to_admin )
        return;
    elseif( $text = $order->get_meta('_payment_message') )
        echo '<div style="border:2px solid #e4e4e4;padding:5px;margin-bottom:12px;"><strong>Note:</span></strong> '.$text.'</div>';
}

Code goes on function.php file of your active child theme (or theme). Tested and works.


Note for PDF Invoice

The rule on stackOverFlow is one question at the time, so for one question one answer, to avoid your question be closed as too broad.

As there is many different PDF invoices plugins for Woocommerce, you will have to read the developer documentation for the WooCommerce PDF Invoices plugin, to display that custom message in the PDF invoices.

Upvotes: 3

Related Questions