cancel subscripion
-
Hi Support Team,
I’m trying to execute a function after a subscription is canceled. I’ve used the hooks provided in the documentation, but they don’t seem to be working as expected.
Is there an alternative approach to store the subscription status in a custom table? Any guidance or examples would be greatly appreciated.
Method 1:add_action('asp_subscription_canceled', 'update_purchase_status_on_cancellation', 10, 2);
function update_purchase_status_on_cancellation($sub_id, $event_data) {
global $wpdb;
$table_name = $wpdb->prefix . 'purchase_product';
$result = $wpdb->update(
$table_name,
array(
'payment_status' => 'Canceled',
'updated_at' => current_time('mysql')
),
array('subscription_id' => $sub_id),
array('%s', '%s'),
array('%s')
);
if ($result === false) {
error_log('Failed to update payment status for subscription ID: ' . $sub_id);
}
}
method 2: on cancel button click called ajaxadd_action('wp_ajax_update_purchase_status_on_cancellation', 'update_purchase_status_on_cancellation');
add_action('wp_ajax_nopriv_update_purchase_status_on_cancellation', 'update_purchase_status_on_cancellation');
function process_subs_cancel() {
global $wpdb;
// Get token from the request
$token = isset($_POST['token']) ? sanitize_text_field($_POST['token']) : '';
if (empty($token)) {
wp_send_json_error(['message' => 'Token is missing']);
}
// Retrieve the post ID or subscription ID using the token
$subscription_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s",
'sub_token', // Replace with the actual meta key for token
$token
)
);
if (!$subscription_id) {
wp_send_json_error(['message' => 'Invalid token']);
}
// Update the payment status
$table_name = $wpdb->prefix . 'purchase_product';
$result = $wpdb->update(
$table_name,
array(
'payment_status' => 'Canceled',
'updated_at' => current_time('mysql'),
),
array('subscription_id' => $subscription_id),
array('%s', '%s'),
array('%s')
);
if ($result === false) {
error_log('Failed to update payment status for subscription ID: ' . $subscription_id);
wp_send_json_error(['message' => 'Failed to update subscription status']);
}
wp_send_json_success(['message' => 'Subscription canceled successfully']);
}
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- You must be logged in to reply to this topic.