Last active
February 11, 2020 19:18
-
-
Save rvdsteege/af3dd0ec9fde60eda1f8e3435d8fbd07 to your computer and use it in GitHub Desktop.
Pronamic Pay + WooCommerce Subscriptions — Keep subscriptions 'active' instead of going 'on hold' during renewal.
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
/* | |
* WooCommerce Subscriptions - Skip order status 'On hold'. | |
*/ | |
add_filter( 'pronamic_payment_status_update_woocommerce_unknown_to_open', 'pronamic_pay_wcs_skip_on_hold', 10, 2 ); | |
function pronamic_pay_wcs_skip_on_hold( $payment, $can_redirect ) { | |
if ( 'woocommerce' !== $payment->get_source() ) { | |
return; | |
} | |
$order_id = $payment->get_source_id(); | |
if ( ! function_exists( 'wcs_order_contains_renewal' ) || ! wcs_order_contains_renewal( $order_id ) ) { | |
return; | |
} | |
$subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id ); | |
$subscription = array_pop( $subscriptions ); | |
$skip = array( | |
'pronamic_pay_direct_debit_bancontact', | |
'pronamic_pay_direct_debit_ideal', | |
'pronamic_pay_direct_debit_sofort', | |
'pronamic_pay_credit_card', | |
); | |
if ( in_array( $subscription->get_payment_method(), $skip ) ) { | |
if ( method_exists( '\Pronamic\WordPress\Pay\Extensions\WooCommerce\Extension', 'subscription_reactivated' ) ) { | |
remove_action( 'woocommerce_subscription_status_on-hold_to_active', array( '\Pronamic\WordPress\Pay\Extensions\WooCommerce\Extension', 'subscription_reactivated' ), 10, 1 ); | |
} | |
$subscription->update_status( 'active', "Skip status 'on hold' during renewal." ); | |
if ( method_exists( '\Pronamic\WordPress\Pay\Extensions\WooCommerce\Extension', 'subscription_reactivated' ) ) { | |
add_action( 'woocommerce_subscription_status_on-hold_to_active', array( '\Pronamic\WordPress\Pay\Extensions\WooCommerce\Extension', 'subscription_reactivated' ), 10, 1 ); | |
} | |
} | |
} | |
// Filter whether to calculate the subscription next payment date based on last payment (true) on payment status update, | |
// or based on the stored next payment date (false). False prevents the payment date from slipping +/- 5 days each month. | |
add_filter( 'wcs_calculate_next_payment_from_last_payment', 'pronamic_pay_wcs_calculate_next_payment_from_last_payment', 10, 2 ); | |
function pronamic_pay_wcs_calculate_next_payment_from_last_payment( $allow, $subscription ) { | |
$disallow = array( | |
'pronamic_pay_direct_debit_bancontact', | |
'pronamic_pay_direct_debit_ideal', | |
'pronamic_pay_direct_debit_sofort', | |
'pronamic_pay_credit_card', | |
); | |
if ( in_array( $subscription->get_payment_method(), $disallow ) ) { | |
$allow = false; | |
} | |
return $allow; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment