dpcyx08288 2017-04-09 00:55
浏览 34
已采纳

woocommerce_thankyou挂钩不工作

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";
    }
}
  • 写回答

1条回答 默认 最新

  • drpph80800 2017-04-09 05:39
    关注

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

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题