Skip to content
This repository was archived by the owner on Apr 21, 2026. It is now read-only.

Commit 472059d

Browse files
committed
🚚 Refactors order status webhook handling
Consolidates order status webhook triggers into a single action. This change simplifies the webhook implementation by using the `woocommerce_order_status_changed` hook instead of individual status hooks. It also introduces a status mapping to align WooCommerce statuses with expected webhook statuses and skips webhooks for draft orders. Adds logging for webhook attempts and results to order notes.
1 parent 259d171 commit 472059d

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

‎includes/class-frak-woocommerce.php‎

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ public static function instance() {
1414
private function __construct() {
1515
add_action('woocommerce_thankyou', array($this, 'track_purchase'));
1616

17-
// Add webhook handlers for order status changes
18-
add_action('woocommerce_order_status_completed', array($this, 'send_order_webhook'));
19-
add_action('woocommerce_order_status_processing', array($this, 'send_order_webhook'));
20-
add_action('woocommerce_payment_complete', array($this, 'send_order_webhook'));
17+
// Add webhook handler for all order status changes
18+
add_action('woocommerce_order_status_changed', array($this, 'handle_order_status_change'), 10, 4);
2119
}
2220

2321
public function track_purchase($order_id) {
@@ -71,35 +69,60 @@ private function get_tracking_script($customer_id, $order_id, $order_key) {
7169
}
7270

7371
/**
74-
* Send webhook when order status changes
72+
* Handle order status changes
7573
*
76-
* @param int $order_id Order ID.
74+
* @param int $order_id Order ID.
75+
* @param string $old_status Old status.
76+
* @param string $new_status New status.
77+
* @param object $order Order object.
7778
*/
78-
public function send_order_webhook($order_id) {
79+
public function handle_order_status_change($order_id, $old_status, $new_status, $order) {
7980
// Check if webhook secret is configured
8081
$webhook_secret = get_option('frak_webhook_secret');
8182
if (empty($webhook_secret)) {
8283
return;
8384
}
8485

85-
// Get order
86-
$order = wc_get_order($order_id);
87-
if (!$order) {
86+
// Define status mapping (WooCommerce status => Frak status)
87+
$status_map = array(
88+
'completed' => 'confirmed',
89+
'processing' => 'pending',
90+
'on-hold' => 'pending',
91+
'pending' => 'pending',
92+
'cancelled' => 'cancelled',
93+
'refunded' => 'refunded',
94+
'failed' => 'cancelled',
95+
);
96+
97+
// Define statuses to skip
98+
$skip_statuses = array(
99+
'checkout-draft', // Draft orders during checkout
100+
'auto-draft', // Auto-draft orders
101+
);
102+
103+
// Skip if status is in skip list
104+
if (in_array($new_status, $skip_statuses)) {
105+
$order->add_order_note(sprintf(__('Frak: Skipping webhook for status: %s', 'frak'), $new_status));
88106
return;
89107
}
90108

91-
// Get order status and token
92-
$status = $order->get_status();
109+
// Map the status, default to 'pending' if not mapped
110+
$webhook_status = isset($status_map[$new_status]) ? $status_map[$new_status] : 'pending';
111+
112+
// Get order token
93113
$token = $order->get_order_key();
94114

115+
// Log the webhook attempt
116+
$order->add_order_note(sprintf(__('Frak: Sending webhook with status: %s', 'frak'), $webhook_status));
117+
95118
// Send webhook
96-
$result = Frak_Webhook_Helper::send($order_id, $status, $token);
119+
$result = Frak_Webhook_Helper::send($order_id, $webhook_status, $token);
97120

98121
// Log result
99122
if ($result['success']) {
100-
$order->add_order_note(__('Frak webhook sent successfully', 'frak'));
123+
$order->add_order_note(__('Frak: Webhook sent successfully', 'frak'));
101124
} else {
102-
$order->add_order_note(sprintf(__('Frak webhook failed: %s', 'frak'), $result['error']));
125+
$order->add_order_note(sprintf(__('Frak: Webhook failed: %s', 'frak'), $result['error']));
103126
}
104127
}
105128
}

0 commit comments

Comments
 (0)