Skip to content

Instantly share code, notes, and snippets.

@MCKLtech
Created May 25, 2020 14:20
Show Gist options
  • Save MCKLtech/71100dcd413c556eb67d7a401dc6eda3 to your computer and use it in GitHub Desktop.
Save MCKLtech/71100dcd413c556eb67d7a401dc6eda3 to your computer and use it in GitHub Desktop.
An overview of how to use Gravity Forms to cancel a WooCommerce Subscription programically
/* This code is NOT safe for production usage. Do not use in live systems */
/* Add to functions.php */
add_action( 'gform_after_submission', 'wc_demo_cancel_sub', 10, 2 );
function wc_demo_cancel_sub( $entry, $form ) {
//Get the subscription id from the form
//We assume the field wc_sub_id contains the subscription ID
$subscription_id = $entry['wc_sub_id'];
//Do some error handling i.e. Ensure it exists, is an int etc
//Fetch the Subscription
$subscription = wcs_get_subscription( $subscription_id );
//Cancel the subscription (Again, should be more error handling here)
$subscription->update_status('cancelled');
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment