为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
GCT1015 2022-12-11 09:20:04
我的问题是这样的: - 根据产品属性和付款方式在感谢页面上打印文本我有这段完美运行的代码: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!提前致谢!
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

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

编辑


在页面顶部显示文本,在订单详细信息之前


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,

//img1.sycdn.imooc.com//639530690001924206410464.jpg

查看完整回答
反对 回复 2022-12-11
  • 1 回答
  • 0 关注
  • 49 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信