2017-04-09 220 views
0

当我将下面的代码直接粘贴到thankyou.php时,它工作得很好。但是当我试图将其挂接到woocommerce_thankyou时,没有任何反应。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"; 
    } 
} 
+0

你是什么意思的'钩'? – Neil

+0

你如何试图将其挂钩? –

回答

2

首先你要添加的功能和钩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"; 
    } 
} 

希望这有助于!

+0

这...工作。非常感谢!!!虽然我不完全确定为什么 –