Created
November 17, 2019 10:32
-
-
Save passatgt/e344f39c5dedbbee657ec6c53f4a6175 to your computer and use it in GitHub Desktop.
Create a new custom Webhook in WooCommerce with a custom response
This file contains hidden or 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 | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WC_Sample_Webhook', false ) ) : | |
class WC_Sample_Webhook { | |
//Init notices | |
public static function init() { | |
//Create topics and pair them with action to call them | |
add_filter( 'woocommerce_webhook_topic_hooks', array( __CLASS__, 'add_topics'), 10, 2 ); | |
//Add topics to the admin dropdown menu when you create the webhook | |
add_filter( 'woocommerce_webhook_topics' , array( __CLASS__, 'add_topics_admin_menu'), 10, 1 ); | |
//Register the name of your resource as a valid webhook resource | |
add_filter( 'woocommerce_valid_webhook_resources', array( __CLASS__, 'add_resource'), 10, 1 ); | |
//Register custom event names as valid events | |
add_filter( 'woocommerce_valid_webhook_events', array( __CLASS__, 'add_event'), 10, 1 ); | |
//Create custom response | |
add_filter( 'woocommerce_webhook_payload', array( __CLASS__, 'create_payload'), 10, 4 ); | |
} | |
public static function add_topics($topic_hooks, $webhook) { | |
if ( 'your_resource' == $webhook->get_resource() ) { | |
$topic_hooks = array( | |
'your_resource.created' => array('your_resource_action_created'), //The your_resource.created webhook topic will run when you call do_action('your_resource_action_created') in your plugin | |
'your_resource.updated' => array('your_resource_action_updated'), | |
'your_resource.custom_event' => array('your_resource_action_custom_event'), | |
); | |
} | |
return $topic_hooks; | |
} | |
public static function add_topics_admin_menu( $topics ) { | |
$front_end_topics = array( | |
'your_resource.created' => 'Your Resource - created', | |
'your_resource.updated' => 'Your Resource - updated', | |
'your_resource.custom_event' => 'Your Resource - custom event', | |
); | |
return array_merge( $topics, $front_end_topics ); | |
} | |
//You need to register your resource name, because only coupon', 'customer', 'order', 'product' are valid by default | |
public static function add_resource( $resources ) { | |
$resources[] = 'your_resource'; | |
return $resources; | |
} | |
//You need to register any custom event names, because only 'created', 'updated', 'deleted', 'restored' are valid by default | |
public static function add_event( $events ) { | |
$events[] = 'custom_event'; | |
return $events; | |
} | |
public static function create_payload( $payload, $resource, $resource_id, $id ) { | |
if ( 'your_resource' == $resource && empty( $payload ) ) { | |
$webhook = new WC_Webhook( $id ); | |
$event = $webhook->get_event(); | |
$payload = array('test' => 123); | |
} | |
return $payload; | |
} | |
} | |
WC_Sample_Webhook::init(); | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment