本文介绍了woocommerce_order_status_completed 未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个自定义插件,在 woocommerce 订单完成后执行一些操作,但我无法让这个钩子工作.我可以看到这个问题被问了很多次.

I want to write a custom plugin that does some action after woocommerce order is completed, but I can't get this hook to work. I can see this question asked many times.

像这里:https://wordpress.stackexchange.com/questions/134463/woocommerce-order-status-completed-action-hook-not-working

这里:https://wordpress.org/support/topic/woocommerce_order_status_completed-不工作

这里:https://wordpress.org/support/topic/woocommerce_order_status_completed-action-hook-not-working

但我无法回答这些人收到的答案.

But I cannot help myself with answers that these guys received.

我尝试通过几种不同的方式添加操作:

I tried to add the action a few different ways:

add_action( 'woocommerce_order_status_completed', 'ikwoocommerceorderstatuscompleted_func');

add_action( 'woocommerce_order_status_completed', array($this,'ikwoocommerceorderstatuscompleted_func'), 10, 1);

add_action( 'woocommerce_order_status_completed', array(&$this,'ikwoocommerceorderstatuscompleted_func'), 10, 1);

还尝试了一个类:

class IKHooks {
    function __construct() {
        add_action( 'woocommerce_order_status_completed', array($this,'ikwoocommerceorderstatuscompleted_func'), 10, 1);
    }

    public function ikwoocommerceorderstatuscompleted_func( $order_id ) {

    }
}

我什至试图将动作放在课堂之外:

I even tried to put the action outside of the class:

add_action( 'woocommerce_order_status_completed', array(IKHooks,'ikwoocommerceorderstatuscompleted_func'), 10, 1);

这些示例都不起作用.:(

None of these examples work. :(

推荐答案

在调用钩子之前检查以下步骤.

Check the following steps before calling your hook.

  1. 检查是否发送了订单完成电子邮件.

  1. Check if order completion email is sent.

钩子在插件文件或主题中正确注册functions.php

Hook is properly registered in plugin file or theme functions.php

add_action( 'woocommerce_order_status_completed','callback_function_name' );

function callback_function_name(){
  global $wp_filter;
  print_r($wp_filter);
  exit;
}

检查回调函数的名称是否在钩子数组中:

Check if the name of your callback function is in the hook array:

[woocommerce_order_status_completed] => Array
    (
        [10] => Array
            (
                [wc_paying_customer] => Array
                    (
                        [function] => wc_paying_customer
                        [accepted_args] => 1
                    )

                [wc_downloadable_product_permissions] => Array
                    (
                        [function] => wc_downloadable_product_permissions
                        [accepted_args] => 1
                    )

                [callback_function_name] => Array
                    (
                        [function] => callback_function_name
                        [accepted_args] => 3
                    )

            )

    )

如果您找到它,则一切正常,这意味着您的主题或 functions.php 文件可能存在问题.检查文件中的钩子或回调函数,然后查找 remove_actionremove_all_actions 这可能是阻止调用钩子的原因.

If you find it then everything is ok, it means that probably there's an issue with your theme or functions.php file. Check for the hook or callback function in your files and then look for remove_action or remove_all_actions that's probably what's preventing your hook from being called.

您也可以通过这种方式查看

You can also check in this way

add_action( 'woocommerce_order_status_completed', 'callback_function_name', 1);

将钩子的优先级从 10 更改为 1,使其比任何其他操作或钩子先被调用.

Change the priority of your hook from 10 to 1 so it is called first than any other action or hook.

这篇关于woocommerce_order_status_completed 未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 09:04