Skip to content

Instantly share code, notes, and snippets.

@abegit
Created February 2, 2016 20:11
Show Gist options
  • Save abegit/74e691d877cadc37d7b6 to your computer and use it in GitHub Desktop.
Save abegit/74e691d877cadc37d7b6 to your computer and use it in GitHub Desktop.
WooCommerce Hooks for Order Status Changes
function mysite_pending($order_id) {
error_log("$order_id set to PENDING", 0);
}
function mysite_failed($order_id) {
error_log("$order_id set to FAILED", 0);
}
function mysite_hold($order_id) {
error_log("$order_id set to ON HOLD", 0);
}
function mysite_processing($order_id) {
error_log("$order_id set to PROCESSING", 0);
}
function mysite_completed($order_id) {
error_log("$order_id set to COMPLETED", 0);
}
function mysite_refunded($order_id) {
error_log("$order_id set to REFUNDED", 0);
}
function mysite_cancelled($order_id) {
error_log("$order_id set to CANCELLED", 0);
}
add_action( ‘woocommerce_order_status_pending’, ‘mysite_pending’);
add_action( ‘woocommerce_order_status_failed’, ‘mysite_failed’);
add_action( ‘woocommerce_order_status_on-hold’, ‘mysite_hold’);
// Note that it’s woocommerce_order_status_on-hold, not on_hold.
add_action( ‘woocommerce_order_status_processing’, ‘mysite_processing’);
add_action( ‘woocommerce_order_status_completed’, ‘mysite_completed’);
add_action( ‘woocommerce_order_status_refunded’, ‘mysite_refunded’);
add_action( ‘woocommerce_order_status_cancelled’, ‘mysite_cancelled’);
@FreshLondon
Copy link

Just to note that your ‘ should be '.
A small difference but a crucial one.

Thanks for the script! <3

@michaelbarley
Copy link

@abegit

Where does it get $order_id from ? I have put this in my functions.php, getting the error: Undefined variable: order_id.

Thanks

@smiell
Copy link

smiell commented May 10, 2024

@abegit

Where does it get $order_id from ? I have put this in my functions.php, getting the error: Undefined variable: order_id.

Thanks

I am also wondering many times ;) This error only comes from IDE, but I think $order_id come from deeper...

@FreshLondon
Copy link

FreshLondon commented May 11, 2024

@michaelbarley @smiell
technically the add_action() is hooking into an existing do_action() which has multiple parameters.
These are:

  • hook name,
  • callback function,
  • priority,
  • accepted arguments.

Because the default priority is 10 and the accepted argument is 1 for any 'add_action', we do not have to specify these if we are not changing them. For this reason, only two parameters are being specified in the above actions (hook name and callback).
That default 'argument' in the above hooks is $order_id.

If you wish for your IDE to stop showing this as an error you can include WooCommerce in it's PHP frameworks, allowing your IDE to see the WooCommerce code and spot that this is not an error.

@smiell
Copy link

smiell commented May 11, 2024

@michaelbarley @smiell technically the add_action() is hooking into an existing do_action() which has multiple parameters. These are:

  • hook name,
  • callback function,
  • priority,
  • accepted arguments.

Because the default priority is 10 and the accepted argument is 1 for any 'add_action', we do not have to specify these if we are not changing them. That default argument in the above hooks is $order_id.

If you wish for your IDE to stop showing this as an error you can include WooCommerce in it's PHP frameworks, allowing your IDE to see the WooCommerce code and spot that this is not an error.

Thank you for your explanation ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment