Last active
August 15, 2019 03:25
-
-
Save jchristopher/134c04819f1346bc6b0b6c9c174a5bff to your computer and use it in GitHub Desktop.
Prevent EDD Software Licensing Recurring renewal when associated subscription is failing
This file contains hidden or 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
<?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