Skip to content

Instantly share code, notes, and snippets.

@jchristopher jchristopher/functions.php
Last active Apr 15, 2019

Embed
What would you like to do?
Prevent EDD Software Licensing Recurring renewal when associated subscription is failing
<?php
// Prevent license renewal when associated subscription is failing
// (it will cause double charges and double subscriptions and waste a lot of your time)
add_action( 'edd_pre_add_to_cart', function( $download_id, $options ) {
// Only applicable if this is a renewal.
if ( empty( $options['is_renewal'] ) ) {
return;
}
// Handle action parameters.
$download_id = absint( $download_id );
$license_id = absint( $options['license_id'] );
$license_key = sanitize_text_field( $options['license_key'] );
// We rely on a number of classes to accomplish this task.
if (
! class_exists( 'EDD_Customer' )
|| ! class_exists( 'EDD_Recurring_Subscriber' )
|| ! function_exists( 'edd_software_licensing' )
) {
return;
}
// Retrieve subscriptions for the subscriber.
$subscriber = new EDD_Recurring_Subscriber( get_current_user_id(), true );
$subscriptions = $subscriber->get_subscriptions( $download_id );
if ( empty( $subscriptions ) ) {
return;
}
$licence_being_renewed = edd_software_licensing()->get_license( $license_id );
// Loop through the subscriptions to find the associated license keys.
foreach ( $subscriptions as $subscription ) {
// If this parent payment ID doesn't match the payment ID of the license being renewed, it's inapplicable.
if ( $subscription->parent_payment_id != $licence_being_renewed->payment_id ) {
continue;
}
// We have the subscription for the license being renewed.
// If the status is failing we need to prevent adding to cart.
if ( 'stripe' == $subscription->gateway && 'failing' == $subscription->status ) {
wp_safe_redirect( site_url( 'account/payment-information/' ) );
die();
}
}
}, 5, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.