| 1 |
jQuery( document ).ready( () => { |
| 2 |
let sdevs_enable_subscription = jQuery( 'input#subscrpt_enable' ); |
| 3 |
sdevs_enable_subscription.change( () => { |
| 4 |
if ( sdevs_enable_subscription.is( ':checked' ) ) { |
| 5 |
jQuery( '.show_if_subscription' ).show(); |
| 6 |
} else { |
| 7 |
jQuery( '.show_if_subscription' ).hide(); |
| 8 |
} |
| 9 |
} ); |
| 10 |
if ( sdevs_enable_subscription.is( ':checked' ) ) { |
| 11 |
jQuery( '.show_if_subscription' ).show(); |
| 12 |
} else { |
| 13 |
jQuery( '.show_if_subscription' ).hide(); |
| 14 |
} |
| 15 |
|
| 16 |
jQuery( document ).on( 'woocommerce_variations_loaded', () => { |
| 17 |
let total_variations = JSON.parse( |
| 18 |
jQuery( '.woocommerce_variations' ).attr( 'data-total' ) |
| 19 |
); |
| 20 |
for ( let index = 0; index < total_variations; index++ ) { |
| 21 |
let element = document.getElementById( |
| 22 |
'subscrpt_enable[' + index + ']' |
| 23 |
); |
| 24 |
if ( element && element.checked ) { |
| 25 |
jQuery( '.show_if_subscription_' + index ).show(); |
| 26 |
} else { |
| 27 |
jQuery( '.show_if_subscription_' + index ).hide(); |
| 28 |
} |
| 29 |
} |
| 30 |
} ); |
| 31 |
|
| 32 |
let subscrpt_renewal_process = jQuery( '#subscrpt_renewal_process' ); |
| 33 |
subscrpt_renewal_process.change( () => { |
| 34 |
if ( subscrpt_renewal_process.val() === 'manual' ) { |
| 35 |
jQuery( '#sdevs_renewal_cart_tr' ).show(); |
| 36 |
jQuery( '#subscrpt_stripe_auto_renew_tr' ).hide(); |
| 37 |
jQuery( '#subscrpt_auto_renewal_toggle_tr' ).hide(); |
| 38 |
} else { |
| 39 |
jQuery( '#sdevs_renewal_cart_tr' ).hide(); |
| 40 |
jQuery( '#subscrpt_stripe_auto_renew_tr' ).show(); |
| 41 |
jQuery( '#subscrpt_auto_renewal_toggle_tr' ).show(); |
| 42 |
} |
| 43 |
} ); |
| 44 |
if ( subscrpt_renewal_process.val() === 'manual' ) { |
| 45 |
jQuery( '#sdevs_renewal_cart_tr' ).show(); |
| 46 |
jQuery( '#subscrpt_stripe_auto_renew_tr' ).hide(); |
| 47 |
jQuery( '#subscrpt_auto_renewal_toggle_tr' ).hide(); |
| 48 |
} else { |
| 49 |
jQuery( '#sdevs_renewal_cart_tr' ).hide(); |
| 50 |
jQuery( '#subscrpt_stripe_auto_renew_tr' ).show(); |
| 51 |
jQuery( '#subscrpt_auto_renewal_toggle_tr' ).show(); |
| 52 |
} |
| 53 |
|
| 54 |
// Handle "select all" checkbox for subscription list |
| 55 |
jQuery( '#cb-select-all-1' ).on( 'change', function() { |
| 56 |
jQuery( 'input[name="subscription_ids[]"]' ).prop( 'checked', this.checked ); |
| 57 |
} ); |
| 58 |
|
| 59 |
// Update "select all" checkbox when individual checkboxes change |
| 60 |
jQuery( 'input[name="subscription_ids[]"]' ).on( 'change', function() { |
| 61 |
var totalCheckboxes = jQuery( 'input[name="subscription_ids[]"]' ).length; |
| 62 |
var checkedCheckboxes = jQuery( 'input[name="subscription_ids[]"]:checked' ).length; |
| 63 |
|
| 64 |
if ( checkedCheckboxes === 0 ) { |
| 65 |
jQuery( '#cb-select-all-1' ).prop( 'indeterminate', false ).prop( 'checked', false ); |
| 66 |
} else if ( checkedCheckboxes === totalCheckboxes ) { |
| 67 |
jQuery( '#cb-select-all-1' ).prop( 'indeterminate', false ).prop( 'checked', true ); |
| 68 |
} else { |
| 69 |
jQuery( '#cb-select-all-1' ).prop( 'indeterminate', true ); |
| 70 |
} |
| 71 |
} ); |
| 72 |
|
| 73 |
// Handle bulk action form submission |
| 74 |
jQuery( '#subscriptions-form' ).on( 'submit', function( e ) { |
| 75 |
// Check if this is a bulk action submission by checking the submitter |
| 76 |
var submitter = e.originalEvent ? e.originalEvent.submitter : null; |
| 77 |
var isBulkAction = submitter && ( submitter.name === 'bulk_action' || submitter.name === 'bulk_action2' ); |
| 78 |
|
| 79 |
if ( ! isBulkAction ) { |
| 80 |
// This is a filter submission, allow it to proceed |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
// Prevent default form submission for bulk actions |
| 85 |
e.preventDefault(); |
| 86 |
|
| 87 |
var action = jQuery( 'select[name="action"]' ).val(); |
| 88 |
var action2 = jQuery( 'select[name="action2"]' ).val(); |
| 89 |
var selectedAction = action !== '-1' ? action : action2; |
| 90 |
|
| 91 |
if ( selectedAction === '-1' ) { |
| 92 |
alert( 'Please select a bulk action.' ); |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
var checkedBoxes = jQuery( 'input[name="subscription_ids[]"]:checked' ); |
| 97 |
if ( checkedBoxes.length === 0 ) { |
| 98 |
alert( 'Please select at least one subscription.' ); |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
// Confirm destructive actions |
| 103 |
if ( selectedAction === 'delete' ) { |
| 104 |
if ( ! confirm( 'Are you sure you want to permanently delete the selected subscriptions? This action cannot be undone.' ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
} else if ( selectedAction === 'trash' ) { |
| 108 |
if ( ! confirm( 'Are you sure you want to move the selected subscriptions to trash?' ) ) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Handle bulk action via AJAX |
| 114 |
handleBulkAction( selectedAction, checkedBoxes ); |
| 115 |
} ); |
| 116 |
|
| 117 |
// AJAX function to handle bulk actions |
| 118 |
function handleBulkAction( action, checkedBoxes ) { |
| 119 |
var subscriptionIds = []; |
| 120 |
checkedBoxes.each( function() { |
| 121 |
subscriptionIds.push( jQuery( this ).val() ); |
| 122 |
} ); |
| 123 |
|
| 124 |
// Show loading state |
| 125 |
var submitButton = jQuery( 'input[name="bulk_action"]:focus, input[name="bulk_action2"]:focus' ); |
| 126 |
var originalText = submitButton.val(); |
| 127 |
submitButton.val( 'Processing...' ).prop( 'disabled', true ); |
| 128 |
|
| 129 |
// Make AJAX request |
| 130 |
jQuery.ajax( { |
| 131 |
url: wp_subscription_ajax.ajaxurl, |
| 132 |
type: 'POST', |
| 133 |
data: { |
| 134 |
action: 'wp_subscription_bulk_action', |
| 135 |
bulk_action: action, |
| 136 |
subscription_ids: subscriptionIds, |
| 137 |
nonce: wp_subscription_ajax.nonce |
| 138 |
}, |
| 139 |
success: function( response ) { |
| 140 |
if ( response.success ) { |
| 141 |
// Show success message |
| 142 |
showAdminNotice( response.data.message, 'success' ); |
| 143 |
// Reload the page to reflect changes |
| 144 |
setTimeout( function() { |
| 145 |
window.location.reload(); |
| 146 |
}, 1000 ); |
| 147 |
} else { |
| 148 |
// Show error message |
| 149 |
showAdminNotice( response.data.message || 'An error occurred while processing the bulk action.', 'error' ); |
| 150 |
} |
| 151 |
}, |
| 152 |
error: function() { |
| 153 |
showAdminNotice( 'An error occurred while processing the bulk action.', 'error' ); |
| 154 |
}, |
| 155 |
complete: function() { |
| 156 |
// Reset button state |
| 157 |
submitButton.val( originalText ).prop( 'disabled', false ); |
| 158 |
} |
| 159 |
} ); |
| 160 |
} |
| 161 |
|
| 162 |
// Function to show admin notices |
| 163 |
function showAdminNotice( message, type ) { |
| 164 |
var noticeClass = type === 'success' ? 'notice-success' : 'notice-error'; |
| 165 |
var notice = jQuery( '<div class="notice ' + noticeClass + ' is-dismissible"><p>' + message + '</p></div>' ); |
| 166 |
|
| 167 |
// Insert notice at the top of the page |
| 168 |
jQuery( '.wp-subscription-admin-content' ).prepend( notice ); |
| 169 |
|
| 170 |
// Auto-dismiss after 5 seconds |
| 171 |
setTimeout( function() { |
| 172 |
notice.fadeOut( function() { |
| 173 |
jQuery( this ).remove(); |
| 174 |
} ); |
| 175 |
}, 5000 ); |
| 176 |
} |
| 177 |
} ); |
| 178 |
|
| 179 |
function hellochange( index ) { |
| 180 |
if ( document.getElementById( 'subscrpt_enable[' + index + ']' ).checked ) { |
| 181 |
jQuery( '.show_if_subscription_' + index ).show(); |
| 182 |
} else { |
| 183 |
jQuery( '.show_if_subscription_' + index ).hide(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
let subscrpt_product_type = jQuery( '#product-type' ); |
| 188 |
let latest_value_of_subscrpt_product_type = subscrpt_product_type.val(); |
| 189 |
|
| 190 |
subscrpt_product_type.change( () => { |
| 191 |
if ( |
| 192 |
'simple' === latest_value_of_subscrpt_product_type && |
| 193 |
'simple' !== subscrpt_product_type.val() && |
| 194 |
'variable' !== subscrpt_product_type.val() |
| 195 |
) { |
| 196 |
const confirmTypeChange = confirm( |
| 197 |
"Are you sure to change the product type ? If product type changed then You'll lose related subscriptions beacuse of they can't be renewed !" |
| 198 |
); |
| 199 |
if ( confirmTypeChange ) { |
| 200 |
latest_value_of_subscrpt_product_type = subscrpt_product_type.val(); |
| 201 |
} else { |
| 202 |
subscrpt_product_type.val( 'simple' ); |
| 203 |
} |
| 204 |
} |
| 205 |
} ); |
| 206 |
|