Created
May 25, 2020 14:20
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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