PluginProbe
AI Chatbot for WooCommerce & Live Chat – onWebChat / 3.7.0
AI Chatbot for WooCommerce & Live Chat – onWebChat v3.7.0
3.10.0 3.9.2 3.9.3 3.9.1 3.9.0 3.8.4 3.8.2 3.8.1 3.8.0 3.7.2 3.7.1 3.7.0 3.6.0 3.5.5 trunk 1.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.2 1.0.3 1.0.4 1.0.5 All 49 releases
onwebchat / admin / tabs / woocommerce.php

woocommerce.php in AI Chatbot for WooCommerce & Live Chat – onWebChat 3.7.0, at admin/tabs/woocommerce.php

673 lines 30.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Tab - Product Sync Settings
4 */
5
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9
10 function onwebchat_woocommerce_tab() {
11
12 // Handle form submissions
13 onwebchat_handle_woocommerce_actions();
14
15 // Get current settings
16 $sync_enabled = get_option('onwebchat_wc_sync_enabled', false);
17 $sync_mode = get_option('onwebchat_wc_sync_mode', 'short_fallback_full');
18 $secret = get_option('onwebchat_wc_sync_secret', '');
19 $order_lookup_enabled = get_option('onwebchat_wc_order_lookup_enabled', false);
20
21 // Get sync status
22 $sync_status = array(
23 'last_sync' => get_option('onwebchat_wc_last_bulk_sync', 0),
24 'in_progress' => get_option('onwebchat_wc_bulk_in_progress', false),
25 'done' => get_option('onwebchat_wc_bulk_done', 0),
26 'total' => get_option('onwebchat_wc_bulk_total', 0),
27 'last_sync_start' => get_option('onwebchat_wc_last_sync_start', 0),
28 );
29
30 // Calculate cooldown status
31 $cooldown_period = 5 * 60; // 5 minutes // 5min // 5 * 60 //also in the file woocommerce-sync.php
32 $time_since_last_sync = time() - $sync_status['last_sync_start'];
33 $is_in_cooldown = ($time_since_last_sync < $cooldown_period) && $sync_status['last_sync_start'] > 0;
34 $cooldown_remaining = $is_in_cooldown ? ceil(($cooldown_period - $time_since_last_sync) / 60) : 0;
35
36 ?>
37
38 <?php if (isset($_GET['wc_saved']) && $_GET['wc_saved'] == '1'): ?>
39 <div class="notice notice-success is-dismissible">
40 <p><strong>WooCommerce settings saved successfully!</strong></p>
41 </div>
42 <?php endif; ?>
43
44 <?php
45 // Check for authentication errors
46 $auth_error = get_transient('onwebchat_wc_auth_error');
47 if ($auth_error):
48 ?>
49 <div class="notice notice-error is-dismissible">
50 <p><strong>⚠️ WooCommerce Sync Authentication Error:</strong></p>
51 <p><?php echo esc_html($auth_error); ?></p>
52 <p>This usually happens when:</p>
53 <ul style="list-style: disc; margin-left: 20px;">
54 <li>You're trying to connect the plugin to a different onWebChat account</li>
55 </ul>
56 <p><strong>Solution:</strong> Please enter your onWebChat account credentials again in the "Connect WooCommerce Sync" section below and click "Connect WooCommerce Sync" button to reconnect.</p>
57 </div>
58 <?php endif; ?>
59
60 <h2>WooCommerce Product Sync</h2>
61 <p>
62 Let your AI chatbot understand your WooCommerce products.
63 When enabled, products are automatically synced and kept up to date, allowing the AI to answer product-related questions accurately.
64 </p>
65
66 <?php if (empty($secret)): ?>
67 <!-- Authentication required section -->
68 <div class="notice notice-warning" style="margin: 15px 0; padding: 15px;">
69 <h3 style="margin-top: 0;">🔐 Connect WooCommerce Sync</h3>
70 <p>
71 To enable WooCommerce sync, please enter your onWebChat account credentials below.
72 </p>
73 <p style="color: #666; font-size: 13px;">
74 <!-- <strong>💡 Tip:</strong> If you originally connected using email/password in the <a href="?page=onwebchat_settings&tab=general">General tab</a>,
75 try unlinking and re-linking your account there. WooCommerce sync will be connected automatically! -->
76 </p>
77
78 <table class="form-table" style="margin-bottom: 0;">
79 <tr>
80 <th scope="row"><label for="onwebchat_wc_email">Email</label></th>
81 <td>
82 <input type="email"
83 id="onwebchat_wc_email"
84 class="regular-text"
85 placeholder="your@email.com"
86 value="<?php echo esc_attr(get_option('onwebchat_plugin_option_user', '')); ?>">
87 </td>
88 </tr>
89 <tr>
90 <th scope="row"><label for="onwebchat_wc_password">Password</label></th>
91 <td>
92 <input type="password"
93 id="onwebchat_wc_password"
94 class="regular-text"
95 placeholder="Your onWebChat password">
96 </td>
97 </tr>
98 </table>
99
100 <p style="margin-top: 15px;">
101 <button type="button" id="onwebchat_wc_connect" class="button button-primary">
102 Connect WooCommerce Sync
103 </button>
104 <span id="onwebchat_wc_connect_status" style="margin-left: 10px;"></span>
105 </p>
106 </div>
107
108 <script type="text/javascript">
109 jQuery(document).ready(function($) {
110 $('#onwebchat_wc_connect').on('click', function() {
111 var email = $('#onwebchat_wc_email').val();
112 var password = $('#onwebchat_wc_password').val();
113
114 if (!email || !password) {
115 alert('Please enter both email and password.');
116 return;
117 }
118
119 var $btn = $(this);
120 var $status = $('#onwebchat_wc_connect_status');
121
122 $btn.prop('disabled', true).text('Connecting...');
123 $status.html('');
124
125 $.ajax({
126 url: ajaxurl,
127 type: 'POST',
128 data: {
129 action: 'onwebchat_wc_connect',
130 email: email,
131 password: password,
132 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
133 },
134 success: function(response) {
135 if (response.success) {
136 $status.html('<span style="color: green;">✓ Connected successfully!</span>');
137 setTimeout(function() {
138 location.reload();
139 }, 1000);
140 } else {
141 // Render the server-supplied error as TEXT (never HTML) so a malicious/MITM'd
142 // API response can't inject markup into the admin page.
143 $status.empty().append(
144 $('<span/>').css('color', 'red').text('' + response.data)
145 );
146 $btn.prop('disabled', false).text('Connect WooCommerce Sync');
147 }
148 },
149 error: function() {
150 $status.html('<span style="color: red;">✗ Connection error. Please try again.</span>');
151 $btn.prop('disabled', false).text('Connect WooCommerce Sync');
152 }
153 });
154 });
155 });
156 </script>
157
158 <?php else: ?>
159 <!-- Connected - show settings -->
160
161 <form action="admin.php?page=onwebchat_settings&tab=woocommerce" method="post">
162 <input type="hidden" name="action" value="save_wc_sync">
163 <?php wp_nonce_field('onwebchat_wc_sync_nonce'); ?>
164
165 <table class="form-table">
166 <tr>
167 <th scope="row" style="line-height: 1 !important;">
168 <label for="onwebchat_wc_sync_enabled">Enable Product Sync</label>
169 </th>
170 <td>
171 <label for="onwebchat_wc_sync_enabled">
172 <input type="checkbox"
173 id="onwebchat_wc_sync_enabled"
174 name="onwebchat_wc_sync_enabled"
175 value="1"
176 <?php checked($sync_enabled, true); ?>>
177 Automatically sync products to onWebChat for AI training
178 </label>
179 <p id="onwebchat_wc_sync_status" style="margin: 8px 0 0 0; font-size: 13px; min-height: 20px; visibility: hidden; opacity: 0;">
180 </p>
181 </td>
182 </tr>
183
184 <tr style="display: none;">
185 <th scope="row">
186 <label for="onwebchat_wc_sync_mode">Description Mode</label>
187 </th>
188 <td>
189 <select id="onwebchat_wc_sync_mode" name="onwebchat_wc_sync_mode" style="width: 350px;">
190 <option value="short_only" <?php selected($sync_mode, 'short_only'); ?>>
191 Short description only
192 </option>
193 <option value="short_fallback_full" <?php selected($sync_mode, 'short_fallback_full'); ?>>
194 Short description (fallback to full)
195 </option>
196 </select>
197 <p class="description">
198 Choose how product descriptions are synced for AI training
199 </p>
200 </td>
201 </tr>
202
203 <?php if (!empty($secret)): ?>
204 <tr style="display: none;">
205 <th scope="row">Connection Status</th>
206 <td>
207 <span style="color: green; font-weight: bold;"> Connected</span>
208 <code style="background: #f5f5f5; padding: 8px 12px; border-radius: 4px; display: inline-block; font-size: 11px; margin-left: 10px;">
209 <?php echo esc_html(substr($secret, 0, 8) . '...' . substr($secret, -4)); ?>
210 </code>
211 <button type="button"
212 id="onwebchat_regenerate_secret"
213 class="button"
214 style="margin-left: 10px;">
215 Disconnect & Reconnect
216 </button>
217 <p class="description">
218 WooCommerce sync is securely connected. Click "Disconnect & Reconnect" to re-authenticate.
219 </p>
220 </td>
221 </tr>
222 <?php endif; ?>
223 </table>
224 </form>
225
226 <hr>
227
228 <h2>AI Order Status Lookup</h2>
229 <p>
230 Let your AI chatbot answer "where is my order?" with live data from WooCommerce.
231 When a customer asks about their order, the AI securely looks it up in real time and replies with the
232 current status, items and tracking, but only after verifying the customer's identity.
233 </p>
234
235 <table class="form-table">
236 <tr>
237 <th scope="row" style="line-height: 1 !important;">
238 <label for="onwebchat_wc_order_lookup_enabled">Enable Order Status Lookup</label>
239 </th>
240 <td>
241 <label for="onwebchat_wc_order_lookup_enabled">
242 <input type="checkbox"
243 id="onwebchat_wc_order_lookup_enabled"
244 name="onwebchat_wc_order_lookup_enabled"
245 value="1"
246 <?php checked($order_lookup_enabled, true); ?>>
247 Allow the AI chatbot to look up live order status (with identity verification)
248 </label>
249 <p id="onwebchat_wc_order_lookup_status" style="margin: 8px 0 0 0; font-size: 13px; min-height: 20px; visibility: hidden; opacity: 0;">
250 </p>
251 </td>
252 </tr>
253 </table>
254
255 <script type="text/javascript">
256 jQuery(document).ready(function($) {
257 $('#onwebchat_wc_order_lookup_enabled').on('change', function() {
258 var $checkbox = $(this);
259 var $status = $('#onwebchat_wc_order_lookup_status');
260 var isEnabled = $checkbox.is(':checked');
261
262 $checkbox.prop('disabled', true);
263
264 $.ajax({
265 url: ajaxurl,
266 type: 'POST',
267 data: {
268 action: 'onwebchat_wc_save_order_lookup',
269 order_lookup_enabled: isEnabled ? '1' : '0',
270 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
271 },
272 success: function(response) {
273 if (response.success) {
274 var autohide = true;
275 if (response.data.warning) {
276 // Local testing mode: saved locally, but the onWebChat server was not notified.
277 $status.empty().append(
278 $('<span/>').css('color', '#996800').text('' + (response.data.message || 'Saved locally; onWebChat server not notified.'))
279 );
280 autohide = false;
281 } else if (response.data.enabled) {
282 $status.html('<span style="color: green;">�
283 AI order status lookup enabled</span>');
284 } else {
285 $status.html('<span style="color: #d63638;"> AI order status lookup disabled</span>');
286 }
287 $status.css('visibility', 'visible').css('opacity', 1);
288 if (autohide) {
289 setTimeout(function() {
290 $status.animate({opacity: 0}, 300, function() {
291 $status.css('visibility', 'hidden');
292 });
293 }, 1700);
294 }
295 } else {
296 // Revert checkbox on error
297 $checkbox.prop('checked', !isEnabled);
298 alert('Error: ' + (response.data || 'Failed to save setting'));
299 }
300 $checkbox.prop('disabled', false);
301 },
302 error: function() {
303 $checkbox.prop('checked', !isEnabled);
304 alert('An error occurred. Please try again.');
305 $checkbox.prop('disabled', false);
306 }
307 });
308 });
309 });
310 </script>
311
312 <hr>
313
314 <h2>Bulk Sync Status</h2>
315
316 <div id="onwebchat_sync_status_display" style="background: #f9f9f9; padding: 20px; border-radius: 8px; max-width: 800px; margin-top: 15px;">
317 <?php if ($sync_status['in_progress']): ?>
318 <p style="margin: 0 0 10px 0;">
319 <strong>⏳ Sync in progress:</strong>
320 <?php echo esc_html($sync_status['done']); ?> / <?php echo esc_html($sync_status['total']); ?> products synced
321 </p>
322 <div style="background: #fff; border: 1px solid #ddd; border-radius: 4px; height: 20px; overflow: hidden;">
323 <div style="background: #2271b1; height: 100%; width: <?php echo $sync_status['total'] > 0 ? ($sync_status['done'] / $sync_status['total'] * 100) : 0; ?>%; transition: width 0.3s;"></div>
324 </div>
325 <?php elseif ($sync_status['last_sync'] > 0): ?>
326 <p id="onwebchat_last_sync_info" style="margin: 0;">
327 <strong>✓ Last bulk sync:</strong>
328 <?php echo esc_html(human_time_diff($sync_status['last_sync'], current_time('timestamp')) . ' ago'); ?>
329 </p>
330 <p id="onwebchat_total_synced_info" style="margin: 10px 0 0 0; color: #666;">
331 Total products synced: <?php echo esc_html($sync_status['total']); ?>
332 </p>
333 <?php else: ?>
334 <p style="margin: 0; color: #666;">
335 No bulk sync has been performed yet.
336 </p>
337 <?php endif; ?>
338
339 <?php if (!$is_in_cooldown): ?>
340 <p style="margin-top: 15px;">
341 <button type="button"
342 id="onwebchat_sync_now"
343 class="button button-primary"
344 <?php echo $sync_status['in_progress'] ? 'disabled' : ''; ?>>
345 <?php echo $sync_status['in_progress'] ? 'Sync in Progress...' : 'Sync All Products Now'; ?>
346 </button>
347 </p>
348 <?php endif; ?>
349
350 <p class="description onwebchat-sync-description" style="margin-top: 10px;">
351 <?php if ($is_in_cooldown): ?>
352 <?php if ($sync_enabled): ?>
353 Bulk sync was completed recently. Your products are already up to date, and any new or updated products will be synced automatically.
354 <?php else: ?>
355 Bulk sync was completed recently. Your products are already up to date. Please enable "Automatically sync products to onWebChat for AI training" above to keep all your products synced automatically.
356 <?php endif; ?>
357 <?php else: ?>
358 Click to sync all existing products. This process runs in the background and may take several minutes depending on your product count.
359 <?php endif; ?>
360 </p>
361
362 <?php
363 // Show reset button if sync appears stuck (done >= total OR in progress for more than 3 minutes)
364 $stuck_sync = $sync_status['in_progress'] && (
365 $sync_status['done'] >= $sync_status['total'] ||
366 (time() - $sync_status['last_sync_start'] > 180)
367 );
368 if ($stuck_sync):
369 ?>
370 <p style="margin-top: 10px;">
371 <button type="button"
372 id="onwebchat_manual_process_batch"
373 class="button button-secondary"
374 style="margin-right: 10px;">
375 Process Batch Manually
376 </button>
377 <button type="button"
378 id="onwebchat_reset_sync_status"
379 class="button">
380 Mark Sync as Complete
381 </button>
382 <span class="description" style="margin-left: 10px;">
383 If sync appears stuck, try processing manually or reset status.
384 </span>
385 </p>
386 <?php endif; ?>
387 </div>
388
389 <script type="text/javascript">
390 jQuery(document).ready(function($) {
391 // Cooldown timer - reload page when cooldown expires
392 <?php if ($is_in_cooldown): ?>
393 var cooldownSeconds = <?php echo ($cooldown_period - $time_since_last_sync); ?>;
394 setTimeout(function() {
395 location.reload();
396 }, cooldownSeconds * 1000);
397 <?php endif; ?>
398
399 // Sync enabled checkbox - save immediately via AJAX
400 $('#onwebchat_wc_sync_enabled').on('change', function() {
401 var $checkbox = $(this);
402 var $status = $('#onwebchat_wc_sync_status');
403 var $syncDescription = $('.onwebchat-sync-description');
404 var isEnabled = $checkbox.is(':checked');
405
406 // Disable checkbox while saving
407 $checkbox.prop('disabled', true);
408
409 $.ajax({
410 url: ajaxurl,
411 type: 'POST',
412 data: {
413 action: 'onwebchat_wc_save_sync_enabled',
414 sync_enabled: isEnabled ? '1' : '0',
415 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
416 },
417 success: function(response) {
418 if (response.success) {
419 if (response.data.enabled) {
420 $status.html('<span style="color: green;">�
421 WooCommerce product sync enabled</span>');
422 // Update the sync description text if in cooldown
423 if ($syncDescription.length) {
424 var currentText = $syncDescription.text().trim();
425 if (currentText.includes('Bulk sync was completed recently')) {
426 $syncDescription.text('Bulk sync was completed recently. Your products are already up to date, and any new or updated products will be synced automatically.');
427 }
428 }
429 } else {
430 $status.html('<span style="color: #d63638;"> WooCommerce product sync disabled</span>');
431 // Update the sync description text if in cooldown
432 if ($syncDescription.length) {
433 var currentText = $syncDescription.text().trim();
434 if (currentText.includes('Bulk sync was completed recently')) {
435 $syncDescription.text('Bulk sync was completed recently. Your products are already up to date. Please enable "Automatically sync products to onWebChat for AI training" above to keep all your products synced automatically.');
436 }
437 }
438 }
439 // Ensure status is visible and fade in smoothly
440 $status.css('visibility', 'visible').css('opacity', 1);
441 // Hide it after 2.5 seconds with fade out
442 setTimeout(function() {
443 $status.animate({opacity: 0}, 300, function() {
444 $status.css('visibility', 'hidden');
445 });
446 }, 1700);
447 } else {
448 // Revert checkbox on error
449 $checkbox.prop('checked', !isEnabled);
450 alert('Error: ' + (response.data || 'Failed to save setting'));
451 }
452 $checkbox.prop('disabled', false);
453 },
454 error: function() {
455 // Revert checkbox on error
456 $checkbox.prop('checked', !isEnabled);
457 alert('An error occurred. Please try again.');
458 $checkbox.prop('disabled', false);
459 }
460 });
461 });
462
463 // Sync now button
464 $('#onwebchat_sync_now').on('click', function() {
465 if ($(this).prop('disabled')) {
466 return;
467 }
468
469 if (!confirm('Sync all published products with onWebChat to train your AI chatbot?')) {
470 return;
471 }
472
473 var $btn = $(this);
474 $btn.prop('disabled', true).text('Syncing products...');
475
476 // Hide the "Last sync" info while syncing
477 $('#onwebchat_last_sync_info').hide();
478 $('#onwebchat_total_synced_info').hide();
479
480 // Show initial syncing message with progress bar
481 $('#onwebchat_sync_status_display').html(
482 '<p style="margin: 0 0 10px 0;"><strong>⏳ Sync in progress:</strong> <span id="sync_progress_text">0 / ? products synced</span></p>' +
483 '<div style="background: #fff; border: 1px solid #ddd; border-radius: 4px; height: 20px; overflow: hidden;">' +
484 '<div id="sync_progress_bar" style="background: #2271b1; height: 100%; width: 0%; transition: width 0.3s;"></div>' +
485 '</div>' +
486 '<p style="margin: 10px 0 0 0; color: #666;">Please wait, this may take several minutes.</p>'
487 );
488
489 // Start polling for progress updates
490 var progressInterval = setInterval(function() {
491 $.ajax({
492 url: ajaxurl,
493 type: 'POST',
494 data: {
495 action: 'onwebchat_wc_get_sync_status',
496 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
497 },
498 success: function(statusResponse) {
499 if (statusResponse.success && statusResponse.data.in_progress) {
500 var done = statusResponse.data.done || 0;
501 var total = statusResponse.data.total || 0;
502 var percentage = total > 0 ? Math.round((done / total) * 100) : 0;
503
504 $('#sync_progress_text').text(done + ' / ' + total + ' products synced');
505 $('#sync_progress_bar').css('width', percentage + '%');
506 }
507 }
508 });
509 }, 2000); // Poll every 2 seconds
510
511 $.ajax({
512 url: ajaxurl,
513 type: 'POST',
514 timeout: 120000, // 2 minute timeout for large syncs
515 data: {
516 action: 'onwebchat_wc_sync_now',
517 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
518 },
519 success: function(response) {
520 clearInterval(progressInterval); // Stop polling
521
522 if (response.success) {
523 var result = response.data.result;
524 var stats = result.stats;
525 var msg = 'Sync completed!\n\n' +
526 'Products processed: ' + result.done + '/' + response.data.total + '\n' +
527 'Created: ' + stats.created + '\n' +
528 'Updated: ' + stats.updated + '\n' +
529 'Unchanged: ' + stats.skipped + '\n' +
530 'Errors: ' + stats.errors;
531 alert(msg);
532 location.reload();
533 } else {
534 alert('Error: ' + response.data);
535 $btn.prop('disabled', false).text('Sync All Products Now');
536 }
537 },
538 error: function(xhr, status, error) {
539 clearInterval(progressInterval); // Stop polling
540 alert('An error occurred: ' + error + '. Please try again.');
541 $btn.prop('disabled', false).text('Sync All Products Now');
542 }
543 });
544 });
545
546 // Manual process batch button (for debugging stuck syncs)
547 $('#onwebchat_manual_process_batch').on('click', function() {
548 var $btn = $(this);
549 $btn.prop('disabled', true).text('Processing...');
550
551 $.ajax({
552 url: ajaxurl,
553 type: 'POST',
554 data: {
555 action: 'onwebchat_wc_manual_process_batch',
556 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
557 },
558 success: function(response) {
559 if (response.success) {
560 alert('Batch processed. Check console/logs for details.');
561 location.reload();
562 } else {
563 alert('Error: ' + response.data);
564 $btn.prop('disabled', false).text('Process Batch Manually');
565 }
566 },
567 error: function() {
568 alert('An error occurred. Please try again.');
569 $btn.prop('disabled', false).text('Process Batch Manually');
570 }
571 });
572 });
573
574 // Reset sync status button
575 $('#onwebchat_reset_sync_status').on('click', function() {
576 if (!confirm('Mark sync as complete? This will reset the progress indicator.')) {
577 return;
578 }
579
580 var $btn = $(this);
581 $btn.prop('disabled', true).text('Resetting...');
582
583 $.ajax({
584 url: ajaxurl,
585 type: 'POST',
586 data: {
587 action: 'onwebchat_wc_reset_sync_status',
588 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
589 },
590 success: function(response) {
591 if (response.success) {
592 location.reload();
593 } else {
594 alert('Error: ' + response.data);
595 $btn.prop('disabled', false).text('Mark Sync as Complete');
596 }
597 },
598 error: function() {
599 alert('An error occurred. Please try again.');
600 $btn.prop('disabled', false).text('Mark Sync as Complete');
601 }
602 });
603 });
604
605 // Regenerate secret button (now "Disconnect" button)
606 $('#onwebchat_regenerate_secret').on('click', function() {
607 if (!confirm('This will disconnect WooCommerce sync. You will need to re-authenticate with your credentials. Continue?')) {
608 return;
609 }
610
611 var $btn = $(this);
612 $btn.prop('disabled', true).text('Disconnecting...');
613
614 $.ajax({
615 url: ajaxurl,
616 type: 'POST',
617 data: {
618 action: 'onwebchat_wc_regenerate_secret',
619 nonce: '<?php echo wp_create_nonce('onwebchat_wc_sync_nonce'); ?>'
620 },
621 success: function(response) {
622 if (response.success) {
623 alert('WooCommerce sync disconnected. Please re-authenticate to continue syncing.');
624 location.reload();
625 } else {
626 alert('Error: ' + response.data);
627 $btn.prop('disabled', false).text('Disconnect & Reconnect');
628 }
629 },
630 error: function() {
631 alert('An error occurred. Please try again.');
632 $btn.prop('disabled', false).text('Disconnect & Reconnect');
633 }
634 });
635 });
636 });
637 </script>
638
639 <?php endif; // End if secret exists ?>
640
641 <?php
642 }
643
644 /**
645 * Handle form submissions for WooCommerce tab
646 */
647 function onwebchat_handle_woocommerce_actions() {
648
649 if (isset($_POST["action"]) && $_POST["action"] == "save_wc_sync") {
650
651 if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'onwebchat_wc_sync_nonce')) {
652 wp_die('Sorry, your nonce did not verify.');
653 }
654
655 if (!current_user_can('manage_options')) {
656 wp_die('Insufficient permissions.');
657 }
658
659 // Save settings
660 $sync_enabled = isset($_POST["onwebchat_wc_sync_enabled"]) ? true : false;
661 $sync_mode = isset($_POST["onwebchat_wc_sync_mode"]) ? sanitize_text_field($_POST["onwebchat_wc_sync_mode"]) : 'short_fallback_full';
662
663 update_option('onwebchat_wc_sync_enabled', $sync_enabled);
664 update_option('onwebchat_wc_sync_mode', $sync_mode);
665
666 // Secret is now obtained via authenticated connection, not auto-generated
667
668 wp_redirect(admin_url('admin.php?page=onwebchat_settings&tab=woocommerce&wc_saved=1'));
669 exit;
670 }
671 }
672
673