Created
April 24, 2023 17:17
-
-
Save slash1andy/e1b1f626c684ab6a35c0fc28030daeed to your computer and use it in GitHub Desktop.
Sends all order contents in WooCommerce webhook when using action.woocommerce_order_status_processing webhook
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
** | |
* 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( 3, 'order' ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment