Skip to content

Instantly share code, notes, and snippets.

@onetarek
Last active February 22, 2023 20:06
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Add new WooComerce Webhook topic only for order completed
<?php
/*
Custom WC Webhook Manager
Add new webhook topic only for WC order completed.
Developed by Md Jahidul Islam ( oneTarek ) https://onetarek.com
*/
//Don't allow direct access
if( ! defined( 'ABSPATH' ) ) exit;
if( ! class_exists( 'Custom_WC_Webhook_Manager' ) ) :
class Custom_WC_Webhook_Manager {
public function __construct() {
add_filter('woocommerce_webhook_topics', array( $this, 'add_custom_webhook_topics' ), 10, 1 );
add_action('woocommerce_order_status_completed', array( $this, 'on_woocommerce_order_status_completed' ), 10, 2 );
add_filter('woocommerce_webhook_payload', array( $this, 'add_custom_webhook_payload' ), 10, 4 );
}
/*
Custom topics must start with 'action.woocommerce_' or 'action.wc_'
described in the function wc_is_webhook_valid_topic
If the topic name starts with 'action' then there must has a wp hook named with the string used after dot(.)
So for this Topic we must have a wp hook 'wc_custom_order_completed'.
Adding custom topic hooks using filter 'woocommerce_webhook_topic_hooks' does not work here.
*/
public function add_custom_webhook_topics( $topics ){
$topics['action.wc_custom_order_completed'] = 'Custom Order completed';
return $topics;
}
//Trigger wc_custom_order_completed hook on woocommerce_order_status_completed hook.
public function on_woocommerce_order_status_completed( $order_id, $order ){
//We could use 'woocommerce_order_status_completed' hook name directly with the topic name.
//But we are not using that direclty because , if in future WC pluign add new topic using that hook.
do_action('wc_custom_order_completed', $order_id, $order );
}
/*
* Set payload for our custom topic.
*/
public function add_custom_webhook_payload( $payload, $resource, $resource_id, $webhook_id ) {
if( isset( $payload['action'] ) && $payload['action'] == 'wc_custom_order_completed' && !empty( $payload['arg'] ) ) {
$webhook = wc_get_webhook( $webhook_id );
// Build the payload with the same user context as the user who created
// the webhook -- this avoids permission errors as background processing
// runs with no user context.
//See build_payload() in woocommerce/includes/class-wc-webhook.php
$current_user = get_current_user_id();
wp_set_current_user( $webhook->get_user_id() );
$version = str_replace( 'wp_api_', '', $webhook->get_api_version() );
$resource = 'order';
$payload = wc()->api->get_endpoint_data( "/wc/{$version}/{$resource}s/{$resource_id}" );
// Restore the current user.
wp_set_current_user( $current_user );
}
return $payload;
}
}//end class
endif;
$Custom_WC_Webhook_Manager = new Custom_WC_Webhook_Manager();
@khoipro
Copy link

khoipro commented Jan 5, 2022

Thank you for your bootstrap.

@Niloys7
Copy link

Niloys7 commented Feb 17, 2023

Hello Bhaiya,
Since order.updated is triggered whenever the status changes, your code is an attractive solution to get the specific status.
This is actually what I am looking for 😄
Thank you.

@gregg-cbs
Copy link

gregg-cbs commented Feb 22, 2023

Hey im not sure if you can help? i have been scouring the internet for hours and hours and im not getting anywhere. I dont know php.

I am using the woocommerce rest api on my node server to listen to webhooks - product, order, customer but it doesnt have any webhook topics for reviews.

How do I add topics or actions for reviews (crud) so that in the woocommerce api i can register webhooks with these topics and receive updates?

Any direction or help would be greatly appreciated, im at an end here :(

@onetarek
Copy link
Author

@Niloys7
I am glad to hear that someone else needs this code. :)

@onetarek
Copy link
Author

@gregg-cbs
I searched and found a plugin WP Webhooks . This plugin is very costly. Did you check this? If this plugin is unable to solve your problem, you have to write custom code.

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