Skip to content

Instantly share code, notes, and snippets.

@gangesh
Created September 10, 2022 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangesh/2979743111641c0b02a45cf23ff098bd to your computer and use it in GitHub Desktop.
Save gangesh/2979743111641c0b02a45cf23ff098bd to your computer and use it in GitHub Desktop.
Custom Woocommerce Webhook based on Shipping Method
<?php
/**
* add_new_topic_hooks will add a new webhook topic hook.
* @param array $topic_hooks Esxisting topic hooks.
*/
function add_new_topic_hooks( $topic_hooks ) {
// Array that has the topic as resource.event with arrays of actions that call that topic.
$new_hooks = array(
'action.wc_custom_new_order' => array(
'wc_custom_new_order',
),
'action.wc_custom_order_update' => array(
'wc_custom_order_update',
)
);
return array_merge( $topic_hooks, $new_hooks );
}
add_filter( 'woocommerce_webhook_topic_hooks', 'add_new_topic_hooks' );
/**
* add_new_topic_events will add new events for topic resources.
* @param array $topic_events Existing valid events for resources.
*/
function add_new_topic_events( $topic_events ) {
// New events to be used for resources.
$new_events = array(
'wc_custom_new_order',
'wc_custom_order_update'
);
return array_merge( $topic_events, $new_events );
}
add_filter( 'woocommerce_valid_webhook_events', 'add_new_topic_events' );
/**
* add_new_webhook_topics adds the new webhook to the dropdown list on the Webhook page.
* @param array $topics Array of topics with the i18n proper name.
*/
function add_new_webhook_topics( $topics ) {
// New topic array to add to the list, must match hooks being created.
$new_topics = array(
'action.wc_custom_new_order' => __( 'Custom New Order', 'woocommerce' ),
'action.wc_custom_order_update' => __( 'Custom Order Update', 'woocommerce' ),
);
return array_merge( $topics, $new_topics );
}
add_filter( 'woocommerce_webhook_topics', 'add_new_webhook_topics' );
/**
* my_order_item_check will check an order when it is created through the checkout form,
* if it has product ID 10603 as one of the items, it will fire off the action `custom_order_filter`
*
* @param int $order_id The ID of the order that was just created.
* @param array $posted_data Array of all of the data that was posted through checkout form.
* @param object $order The order object.
* @return null
*/
function new_order_custom_hook( $order_id, $order ) {
$order = wc_get_order( $order_id );
//$order = new WC_Order( $order_id );
if($order->get_shipping_method() == "Click and Collect") {
return;
}
elseif($order->get_shipping_method() == "Standard Delivery") {
do_action('wc_custom_new_order', $order_id, $order );
}
else {
}
}
function order_update_custom_hook( $order_id, $order ) {
$order = wc_get_order( $order_id );
//$order = new WC_Order( $order_id );
if($order->get_shipping_method() == "Click and Collect") {
return;
}
elseif($order->get_shipping_method() == "Standard Delivery") {
do_action('wc_custom_order_update', $order_id, $order );
}
else {
}
}
add_action( 'woocommerce_new_order', 'new_order_custom_hook', 1, 2 );
add_action( 'save_post_shop_order', 'order_update_custom_hook', 10, 2 );
/**
* Set the proper resource payload for a custom action webhook
*
* @param int $target_webhook_id
* @param string $desired_resource 'order', 'product', 'coupon', or 'customer'
*/
function set_resource_for_webhook_payload_by_webhook_id($target_webhook_id, $desired_resource) {
// Set the desired_resource payload for this webhook ('order', 'product', 'coupon', 'customer')
add_filter('woocommerce_webhook_resource', function($resource, $webhook_id) use ($target_webhook_id, $desired_resource) {
if($webhook_id == $target_webhook_id) {
return $desired_resource;
}
return $resource;
}, 10, 2);
// Need to ensure our event (i.e., action) is seen as valid, as we've changed the default 'action.' prefix for the topic above
add_filter('woocommerce_valid_webhook_events', function($valid_events) use ($target_webhook_id) {
try {
$topic = wc_get_webhook($target_webhook_id)->get_topic();
list($resource, $event) = explode('.', $topic);
if(!empty($event)) {
$valid_events[] = $event;
}
return $valid_events;
} catch (Exception $e) {
return $valid_events;
}
}, 10);
}
// Change the Webhook ID to suit and data source of order, product, coupon or customer
add_action('init', function(){
set_resource_for_webhook_payload_by_webhook_id( 11, 'order' );
set_resource_for_webhook_payload_by_webhook_id( 13, 'order' );
}, 999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment