jQuery(document).ready(function($) { $('#vegavend-edit-store-id').on('click', function() { if (confirm(vegavendSettings.confirmDisconnect)) { $('#vegavend_store_id').val(''); $('#vegavend_store_id').removeAttr('readonly'); $('#vegavend-edit-store-id').hide(); $('#vegavend-save-store-id').show(); // Remove store ID from the database var data = { action: 'vegavend_delete_store_id', vegavend_nonce: vegavendSettings.nonce }; $.post(vegavendSettings.ajaxurl, data, function(response) { if (response.success) { location.reload(); // Reload the page to reflect the disconnected state } else { console.log(vegavendSettings.storeIdDeleteFailed); } }).fail(function(xhr, status, error) { if (xhr.status === 403) { console.log('You do not have permission to perform this action.'); } console.log(vegavendSettings.ajaxFailed + ' ' + status + ', ' + error); }); } }); $('#vegavend-settings-form').on('submit', function(event) { event.preventDefault(); if ($('#vegavend-save-store-id').is(':visible')) { var storeId = $('#vegavend_store_id').val(); var data = { action: 'vegavend_check_store_id', store_id: storeId, vegavend_nonce: vegavendSettings.nonce }; $.post(vegavendSettings.ajaxurl, data, function(response) { if (response.success) { $('#store-connected').show(); $('#store-not-connected').hide(); // Save the store ID in the database $('#vegavend-settings-form').unbind('submit').submit(); } else { $('#store-connected').hide(); $('#store-not-connected').show(); } }).fail(function(xhr, status, error) { if (xhr.status === 403) { console.log('You do not have permission to perform this action.'); } console.log(vegavendSettings.ajaxFailed + ' ' + status + ', ' + error); }); } else if ($('.vegavend-save-settings').is(':visible')) { // Save general settings without disconnecting the store $('#vegavend-settings-form').unbind('submit').submit(); } }); $('#vegavend-general-settings-form').on('submit', function(event) { event.preventDefault(); // Save general settings without disconnecting the store var syncViaCron = $('#vegavend_sync_via_cron').is(':checked') ? 1 : 0; $('').attr({ type: 'hidden', name: 'vegavend_sync_via_cron', value: syncViaCron }).appendTo('#vegavend-general-settings-form'); $('#vegavend-general-settings-form').unbind('submit').submit(); }); // Fetch store ID from the database var fetchData = { action: 'vegavend_get_store_id', vegavend_nonce: vegavendSettings.nonce }; $.post(vegavendSettings.ajaxurl, fetchData, function(response) { if (response.success) { $('#vegavend_store_id').val(response.data.store_id); } else { console.log(vegavendSettings.storeIdFetchFailed); } }).fail(function(xhr, status, error) { if (xhr.status === 403) { console.log('You do not have permission to perform this action.'); } console.log(vegavendSettings.ajaxFailed + ' ' + status + ', ' + error); }); $('#vegavend-sync-all').on('click', function() { if (vegavendSettings.allTasksCompleted && $('#vegavend-sync-all').is(':visible') && confirm(vegavendSettings.confirmSyncAll)) { var data = { action: 'vegavend_sync_all_products', vegavend_nonce: vegavendSettings.nonce }; $.post(vegavendSettings.ajaxurl, data, function(response) { if (response.success) { alert(vegavendSettings.syncAllSuccess); } else { console.log(vegavendSettings.syncAllFailed); } }).fail(function(xhr, status, error) { if (xhr.status === 403) { console.log('You do not have permission to perform this action.'); } console.log(vegavendSettings.ajaxFailed + ' ' + status + ', ' + error); }); } else { alert('All tasks must be completed before syncing.'); } }); // Show or hide the "Sync All" button based on the checkbox state $('#vegavend-sync-checkbox').on('change', function() { if ($(this).is(':checked')) { $('#vegavend-sync-all').show(); } else { $('#vegavend-sync-all').hide(); } }); });