Created
June 29, 2021 13:01
-
-
Save andrewlimaza/fa641acc1891bbd4b2701a8a3f7da3da to your computer and use it in GitHub Desktop.
Adjust Subscription Delay by one month if it's the day before [PayFast Fix]
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
<?php | |
/** | |
* This adjusts the subscription delay (and cost text) by one month if subscription is set to start the following day. | |
* Fixes an issue specifically with PayFast not supporting subscriptions being created for the following day. | |
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
/** | |
* Adjust Subscription Delay Start Date if set to tomorrow. | |
* This fixes an issue for PayFast. | |
* Adjust the start date to be one month ahead if the subscription delay is the day before. | |
*/ | |
function modify_my_own_start_date( $start_date, $order, $subscription_delay ) { | |
if ( ! function_exists( 'pmprosd_convert_date' ) ) { | |
return $start_date; | |
} | |
$sub_delay_date = str_replace( 'T0:0:0', '', pmprosd_convert_date( $subscription_delay ) ); | |
if ( is_pmpro_sub_tomorrow( $sub_delay_date ) ) { | |
$start_date = date( 'Y-m-d', strtotime( $start_date . "+ 1 Month" ) ); | |
} | |
return $start_date; | |
} | |
add_filter( 'pmprosd_modify_start_date', 'modify_my_own_start_date', 10, 3 ); | |
/** | |
* Custom level cost text for Subscriptions Delay. | |
* Unhook the default level cost text, and use our own logic (copy/paste) | |
* Adjusts the subscription delay date on the fly (HTML text only). | |
*/ | |
function modify_my_subscription_delay_text( $cost, $level ) { | |
if ( ! function_exists( 'pmprosd_convert_date' ) ) { | |
return $cost; | |
} | |
remove_filter("pmpro_level_cost_text", "pmprosd_level_cost_text", 10, 2); | |
if ( ! empty( $level->code_id ) ) { | |
$all_delays = pmpro_getDCSDs( $level->code_id ); | |
if ( ! empty( $all_delays ) && ! empty( $all_delays[ $level->id ] ) ) { | |
$subscription_delay = $all_delays[ $level->id ]; | |
} | |
} else { | |
$subscription_delay = get_option( 'pmpro_subscription_delay_' . $level->id, '' ); | |
} | |
// Converted date, let's adjust it too. | |
$sub_delay_date = str_replace( 'T0:0:0', '', pmprosd_convert_date( $subscription_delay ) ); | |
if ( is_pmpro_sub_tomorrow( $sub_delay_date ) ) { | |
$subscription_delay = date( 'Y-m-d', strtotime( $sub_delay_date . "+ 1 Month" ) ); | |
} | |
$find = array( 'Year.', 'Month.', 'Week.', 'Year</strong>.', 'Month</strong>.', 'Week</strong>.', 'Years.', 'Months.', 'Weeks.', 'Years</strong>.', 'Months</strong>.', 'Weeks</strong>.', 'payments.', 'payments</strong>.' ); | |
$replace = array( 'Year', 'Month', 'Week', 'Year</strong>', 'Month</strong>', 'Week</strong>', 'Years', 'Months', 'Weeks', 'Years</strong>', 'Months</strong>', 'Weeks</strong>', 'payments', 'payments</strong>' ); | |
if ( function_exists( 'pmpro_getCustomLevelCostText' ) ) { | |
$custom_text = pmpro_getCustomLevelCostText( $level->id ); | |
} else { | |
$custom_text = null; | |
} | |
if ( empty( $custom_text ) ) { | |
if ( ! empty( $subscription_delay ) && is_numeric( $subscription_delay ) ) { | |
$cost = str_replace( $find, $replace, $cost ); | |
$cost .= ' after your <strong>' . $subscription_delay . ' day trial</strong>.'; | |
} elseif ( ! empty( $subscription_delay ) ) { | |
$subscription_delay = pmprosd_convert_date( $subscription_delay ); | |
$cost = str_replace( $find, $replace, $cost ); | |
$cost .= ' starting ' . date_i18n( get_option( 'date_format' ), strtotime( $subscription_delay, current_time( 'timestamp' ) ) ) . '.'; | |
} | |
} | |
return $cost; | |
} | |
add_filter( 'pmpro_level_cost_text', 'modify_my_subscription_delay_text', 5, 2 ); | |
/** | |
* Check if Subscription Delay is happening tomorrow (1 day). | |
* This is for PayFast (Doesn't support 1 day sub delays, only 2 days +) | |
* @return bool If the sub delay is set for the following day. | |
*/ | |
function is_pmpro_sub_tomorrow( $sub_delay_date ) { | |
$r = false; | |
$tomorrow = date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . " + 1 day" ) ); | |
if ( $tomorrow === $sub_delay_date ) { | |
$r = true; | |
} | |
return $r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment