Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. <?php
  2.  
  3. // Prevent license renewal when associated subscription is failing
  4. // (it will cause double charges and double subscriptions and waste a lot of your time)
  5. add_action( 'edd_pre_add_to_cart', function( $download_id, $options ) {
  6. // Only applicable if this is a renewal.
  7. if ( empty( $options['is_renewal'] ) ) {
  8. return;
  9. }
  10.  
  11. // Handle action parameters.
  12. $download_id = absint( $download_id );
  13. $license_id = absint( $options['license_id'] );
  14. $license_key = sanitize_text_field( $options['license_key'] );
  15.  
  16. // We rely on a number of classes to accomplish this task.
  17. if (
  18. ! class_exists( 'EDD_Customer' )
  19. || ! class_exists( 'EDD_Recurring_Subscriber' )
  20. || ! function_exists( 'edd_software_licensing' )
  21. ) {
  22. return;
  23. }
  24.  
  25. // Retrieve subscriptions for the subscriber.
  26. $subscriber = new EDD_Recurring_Subscriber( get_current_user_id(), true );
  27. $subscriptions = $subscriber->get_subscriptions( $download_id );
  28.  
  29. if ( empty( $subscriptions ) ) {
  30. return;
  31. }
  32.  
  33. $licence_being_renewed = edd_software_licensing()->get_license( $license_id );
  34.  
  35. // Loop through the subscriptions to find the associated license keys.
  36. foreach ( $subscriptions as $subscription ) {
  37.  
  38. // If this parent payment ID doesn't match the payment ID of the license being renewed, it's inapplicable.
  39. if ( $subscription->parent_payment_id != $licence_being_renewed->payment_id ) {
  40. continue;
  41. }
  42.  
  43. // We have the subscription for the license being renewed.
  44. // If the status is failing we need to prevent adding to cart.
  45. if ( 'stripe' == $subscription->gateway && 'failing' == $subscription->status ) {
  46. wp_safe_redirect( site_url( 'account/payment-information/' ) );
  47. die();
  48. }
  49. }
  50. }, 5, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement