woocommerce_thankyou钩不起作用 [英] woocommerce_thankyou hook not working

查看:119
本文介绍了woocommerce_thankyou钩不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将下面的代码直接粘贴到thankyou.php时,它可以很好地工作.但是当我尝试将其挂接到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.

我刚刚开始使用PHP,

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";
    }
}

推荐答案

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

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";
    }
}

希望这会有所帮助!

这篇关于woocommerce_thankyou钩不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆