问题描述
我想编写一个自定义插件,该插件在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.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.
-
检查是否发送了订单完成电子邮件.
Check if order completitioin email is sent.
挂钩已正确注册在插件文件或主题函数中.php
Hook is properly registered in plugin file or theme functions.php
add_action( 'woocommerce_order_status_completed','callback_function_name' );global $wp_filter;print_r($wp_filter);exit;
add_action( 'woocommerce_order_status_completed','callback_function_name' );global $wp_filter;print_r($wp_filter);exit;
检查回调函数的名称是否在hook数组中:
Check if the name of your callback funciton 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
)
)
)
如果找到它,则一切正常,这意味着您的theme或functions.php文件可能存在问题.检查文件中的钩子或回调函数,然后查找remove_action
或remove_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未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!