Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When I paste the code below directly into the thankyou.php, it works perfectly fine. but when I try to hook it into woocommerce_thankyou, nothing happens.

I'm just getting started with PHP,

add_action('woocommerce_thankyou', 'test_1', 10, 1);

function test_1() {
    $paymethod = $order->payment_method_title;
    $orderstat = $order->get_status();

    if (($orderstat == 'completed') && ($paymethod == 'PayPal')) {
        echo "something";
    } elseif (($orderstat == 'processing') && ($paymethod == 'PayPal')) {
        echo "some other shit";
    } elseif (($orderstat == 'pending') && ($paymethod == 'PayPal')) {
        echo "some other shit";
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
440 views
Welcome To Ask or Share your Answers For Others

1 Answer

First of all you have to add the function and hook in functions.php file of your active child theme (or theme). Or also in any plugin PHP files. Secondly you need to create an instance/object of order, to access the data.

add_action('woocommerce_thankyou', 'wh_test_1', 10, 1);

function wh_test_1($order_id) { //<--check this line

    //create an order instance
    $order = wc_get_order($order_id); //<--check this line

    $paymethod = $order->payment_method_title;
    $orderstat = $order->get_status();

    if (($orderstat == 'completed') && ($paymethod == 'PayPal')) {
        echo "something";
    } 
    elseif (($orderstat == 'processing') && ($paymethod == 'PayPal')) {

        echo "some other code";
    } 
    elseif (($orderstat == 'pending') && ($paymethod == 'PayPal')) {
        echo "some other code";
    }
}

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...