woocommerce_thankyou 挂钩不工作 [英] woocommerce_thankyou hook not working

查看:27
本文介绍了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 code";
    } 
    elseif (($orderstat == 'pending') && ($paymethod == 'PayPal')) {
        echo "some other code";
    }
}

希望这有帮助!

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

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