Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

php - woocommerce_order_status_completed not triggered

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.

Like here: https://wordpress.stackexchange.com/questions/134463/woocommerce-order-status-completed-action-hook-not-working

Here: https://wordpress.org/support/topic/woocommerce_order_status_completed-is-not-working

And here: 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);

Also tried with a class:

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. :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Check the following steps before calling your hook.

  1. Check if order completitioin email is sent.

  2. 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;

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
                    )

            )

    )

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);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...