Last active
May 12, 2024 19:42
-
-
Save onetarek/6652c0ceb04c81918b78dca5e495c49a to your computer and use it in GitHub Desktop.
Add new WooComerce Webhook topic only for order completed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
@Niloys7
I am glad to hear that someone else needs this code. :)
@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
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 :(