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

Commit c2d4fd3

Browse files
committed
feat: add WooCommerce integration
1 parent ab44de6 commit c2d4fd3

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

frak-integration.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function frak_register_settings() {
3333
return stripslashes($input);
3434
}
3535
));
36+
register_setting('frak_settings', 'frak_enable_purchase_tracking');
3637
}
3738
add_action('admin_init', 'frak_register_settings');
3839

@@ -73,16 +74,19 @@ function frak_settings_page() {
7374
$app_name = sanitize_text_field($_POST['frak_app_name']);
7475
$logo_url = esc_url_raw($_POST['frak_logo_url']);
7576
$custom_config = stripslashes($_POST['frak_custom_config']);
77+
$enable_tracking = isset($_POST['frak_enable_purchase_tracking']) ? 1 : 0;
7678

7779
// Save everything
7880
update_option('frak_app_name', $app_name);
7981
update_option('frak_logo_url', $logo_url);
8082
update_option('frak_custom_config', $custom_config);
83+
update_option('frak_enable_purchase_tracking', $enable_tracking);
8184
}
8285

8386
$app_name = get_option('frak_app_name', 'Your App Name');
8487
$logo_url = get_option('frak_logo_url', '');
8588
$custom_config = get_option('frak_custom_config', '');
89+
$enable_tracking = get_option('frak_enable_purchase_tracking', 0);
8690

8791
if (empty($custom_config)) {
8892
$custom_config = <<<JS
@@ -163,6 +167,19 @@ function frak_settings_page() {
163167
value="<?php echo esc_url($logo_url); ?>" class="regular-text">
164168
</td>
165169
</tr>
170+
<tr>
171+
<th scope="row">
172+
<label for="frak_enable_purchase_tracking">WooCommerce Purchase Tracking</label>
173+
</th>
174+
<td>
175+
<label>
176+
<input type="checkbox" id="frak_enable_purchase_tracking"
177+
name="frak_enable_purchase_tracking" value="1"
178+
<?php checked($enable_tracking, 1); ?>>
179+
Enable WooCommerce orders tracking
180+
</label>
181+
</td>
182+
</tr>
166183
</table>
167184

168185
<h2>Advanced Configuration</h2>
@@ -235,4 +252,44 @@ function frak_add_to_frontend() {
235252
<script src="https://cdn.jsdelivr.net/npm/@frak-labs/components@latest/cdn/components.js" defer="defer"></script>
236253
<?php
237254
}
238-
add_action('wp_head', 'frak_add_to_frontend');
255+
add_action('wp_head', 'frak_add_to_frontend');
256+
257+
// Add WooCommerce purchase tracking
258+
function frak_add_purchase_tracking($order_id) {
259+
// Early exit if tracking is disabled
260+
if (!get_option('frak_enable_purchase_tracking', 0)) {
261+
return;
262+
}
263+
264+
if (!$order_id) return;
265+
266+
$order = wc_get_order($order_id);
267+
if (!$order) return;
268+
269+
$customer_id = $order->get_user_id();
270+
$order_key = $order->get_order_key();
271+
$transaction_id = $order->get_transaction_id();
272+
273+
echo "<script>
274+
const interactionToken = sessionStorage.getItem('frak-wallet-interaction-token');
275+
276+
const payload = {
277+
customerId: " . json_encode($customer_id) . ",
278+
orderId: " . json_encode($order_id) . ",
279+
token: " . json_encode($order_key) . "
280+
};
281+
282+
fetch('https://backend.frak.id/interactions/listenForPurchase', {
283+
method: 'POST',
284+
headers: {
285+
'Accept': 'application/json',
286+
'Content-Type': 'application/json',
287+
'x-wallet-sdk-auth': interactionToken
288+
},
289+
body: JSON.stringify(payload)
290+
});
291+
</script>";
292+
}
293+
294+
// Hook into WooCommerce
295+
add_action('woocommerce_thankyou', 'frak_add_purchase_tracking', 10, 1);

0 commit comments

Comments
 (0)