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

时间:2020-04-06 15:45:36

标签: php wordpress woocommerce hook hook-woocommerce

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

我有一段完美的代码:

{
    "Question Communicating": "Natural language",
    "interpretation_type": "recognition",
    "output": [
        "test",
        "test2",
        "something"
    ],
    "Question Learning": "Reinforcement"
}

现在,我只想在显示产品选择条件和付款方式(例如bacs)时返回另一条文本。

示例A:

  • 购买的产品-变量9647
  • 选定的付款方式-Bacs

因此,仅在这种情况下,才会显示“谢谢”页面上的文本:

  • 示例文本-感谢您购买变量A-9647-带有付款方式Bacs!

示例B:

  • 购买的产品-变量9648
  • 选定的付款方式-Bacs

因此,仅在这种情况下,才会显示“谢谢”页面上的文本:

  • 示例文本-谢谢您购买变量B-9648-带有付款方式Bacs!

谢谢!

1 个答案:

答案 0 :(得分:2)

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

Result

相关问题