Dom
Dom

Reputation: 453

Woocommerce - Print text on Thank You page based on product attribute and payment method

My question is exactly this: - Print text on Thank You page based on product attribute and payment method

I have this code that works perfectly:

add_action( 'woocommerce_thankyou', 'show_custom_text_by_variation_id', 1 ); 
function show_custom_text_by_variation_id( $order_id ) {
    $order = wc_get_order( $order_id );
    foreach( $order->get_items() as $item ) {
        // Add whatever variation id you want below here.
        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
            echo '<br/>Example text - Thank you for buy VARIABLE A-9647 !<br/>';
        }
        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
            echo '<br/>Example text - Thank you for buy VARIABLE B-9648 !<br/>';
        }
    }
}

Now I would like to return another text only when the condition of product choice is presented together with the type of payment for example bacs.

Example A :

therefore only in this case the text on Thank You page will result:

or

Example B :

therefore only in this case the text on Thank You page will result:

Thanks in advance!

Upvotes: 1

Views: 831

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

Use: $order->get_payment_method();

function action_woocommerce_thankyou( $order_id ) {
    // Get $order object
    $order = wc_get_order( $order_id );

    // Get items
    $items = $order->get_items();

    // Set variable
    $found = false;

    // Set variable
    $output = '';

    // Loop
    foreach ( $items as $item ) {
        // Add whatever variation id you want below here.
        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
            $output = 'Thank you for buy VARIABLE A-9647';
            $found = true;
            break;
        }

        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
            $output = 'Thank you for buy VARIABLE B-9648';
            $found = true;
            break;
        }
    }

    // Get payment method
    $payment_method = $order->get_payment_method();

    // Payment method = basc & found = true
    if ( $payment_method == 'bacs' && $found ) {
        $output .= ' YOUR PAYMENT IS BACS';
    }

    // Print result
    echo $output;
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

EDIT

show text on top of page, before the order details

function change_order_received_text( $str, $order ) {
    // Get items
    $items = $order->get_items();

    // Set variable
    $found = false;

    // Loop
    foreach ( $items as $item ) {
        // Add whatever variation id you want below here.
        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {
            $str = 'Thank you for buy VARIABLE A-9647';
            $found = true;
            break;
        }

        if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {
            $str = 'Thank you for buy VARIABLE B-9648';
            $found = true;
            break;
        }
    }

    // Get payment method
    $payment_method = $order->get_payment_method();

    // Payment method = basc & found = true
    if ( $payment_method == 'bacs' && $found ) {
        $str .= ' YOUR PAYMENT IS BACS';
    }

    return $str;
}
add_filter('woocommerce_thankyou_order_received_text', 'change_order_received_text', 10, 2 );

Result

Upvotes: 2

Related Questions