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