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

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


我有这段完美运行的代码:


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

        }

    }

}

现在,我只想在产品选择条件与支付类型(例如 bacs)一起出现时返回另一个文本。

示例 A:

  • 购买的产品 - 变量 9647

  • 选择的付款方式 - Bacs

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

  • 示例文本 - 感谢您购买 VARIABLE A-9647 - 使用付款方式 Bacs!

或者

示例 B:

  • 购买的产品 - 变量 9648

  • 选择的付款方式 - Bacs

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

  • 示例文本 - 感谢您购买 VARIABLE B-9648 - 使用付款方式 Bacs!

提前致谢!



GCT1015
浏览 73回答 1
1回答

九州编程

利用:$order->get_payment_method();function action_woocommerce_thankyou( $order_id ) {&nbsp; &nbsp; // Get $order object&nbsp; &nbsp; $order = wc_get_order( $order_id );&nbsp; &nbsp; // Get items&nbsp; &nbsp; $items = $order->get_items();&nbsp; &nbsp; // Set variable&nbsp; &nbsp; $found = false;&nbsp; &nbsp; // Set variable&nbsp; &nbsp; $output = '';&nbsp; &nbsp; // Loop&nbsp; &nbsp; foreach ( $items as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; // Add whatever variation id you want below here.&nbsp; &nbsp; &nbsp; &nbsp; if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output = 'Thank you for buy VARIABLE A-9647';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $output = 'Thank you for buy VARIABLE B-9648';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Get payment method&nbsp; &nbsp; $payment_method = $order->get_payment_method();&nbsp; &nbsp; // Payment method = basc & found = true&nbsp; &nbsp; if ( $payment_method == 'bacs' && $found ) {&nbsp; &nbsp; &nbsp; &nbsp; $output .= ' YOUR PAYMENT IS BACS';&nbsp; &nbsp; }&nbsp; &nbsp; // Print result&nbsp; &nbsp; echo $output;}add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );编辑在页面顶部显示文本,在订单详细信息之前function change_order_received_text( $str, $order ) {&nbsp; &nbsp; // Get items&nbsp; &nbsp; $items = $order->get_items();&nbsp; &nbsp; // Set variable&nbsp; &nbsp; $found = false;&nbsp; &nbsp; // Loop&nbsp; &nbsp; foreach ( $items as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; // Add whatever variation id you want below here.&nbsp; &nbsp; &nbsp; &nbsp; if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9647 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $str = 'Thank you for buy VARIABLE A-9647';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ( isset( $item[ 'variation_id' ] ) && $item[ 'variation_id' ] == 9648 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $str = 'Thank you for buy VARIABLE B-9648';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Get payment method&nbsp; &nbsp; $payment_method = $order->get_payment_method();&nbsp; &nbsp; // Payment method = basc & found = true&nbsp; &nbsp; if ( $payment_method == 'bacs' && $found ) {&nbsp; &nbsp; &nbsp; &nbsp; $str .= ' YOUR PAYMENT IS BACS';&nbsp; &nbsp; }&nbsp; &nbsp; return $str;}add_filter('woocommerce_thankyou_order_received_text', 'change_order_received_text', 10,
打开App,查看更多内容
随时随地看视频慕课网APP