Woocommerce - 根据产品属性和付款方式在感谢页面上打印文本

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

我的问题是: - 根据产品属性和支付方式在感谢页面上打印文本

我有完美运行的代码:

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

现在我想 return 只有当产品选择的条件与支付类型一起出现时,例如 bacs。

示例 A:

因此只有在这种情况下,感谢页面上的文本才会显示:

示例 B:

因此只有在这种情况下,感谢页面上的文本才会显示:

提前致谢!

使用:$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 );

编辑

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