woocommerce_thankyou挂钩不工作

Hei*_*eld 1 php wordpress orders woocommerce hook-woocommerce

当我将下面的代码直接粘贴到其中时thankyou.php,它的工作原理非常好.但是当我试图将其挂钩时woocommerce_thankyou,没有任何反应.

我刚开始使用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";
    }
}
Run Code Online (Sandbox Code Playgroud)

Rau*_*pta 7

首先,您必须添加functions.php 活动子主题(或主题)的函数和挂钩文件.或者也可以在任何插件PHP文件中.其次,您需要创建订单的实例/对象,以访问数据.

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 shit";
    } 
    elseif (($orderstat == 'pending') && ($paymethod == 'PayPal')) {
        echo "some other shit";
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!