Last active
June 9, 2023 17:29
-
-
Save dparker1005/a36c083630a9d55aa23ec12bdc63ab01 to your computer and use it in GitHub Desktop.
Give new customers a free trial when purchasing a subscription.
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 | |
/** | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
*/ | |
// Copy from below this line | |
/** | |
* Add a 7 day trial when sending new customers to checkout. | |
*/ | |
function rwstripetrial_checkout_session_params( $checkout_session_params, $price, $customer_id ) { | |
// Only apply trials to subscriptions. | |
if ( $checkout_session_params['mode'] === 'subscription' ) { | |
// Only allow a trial if the customer has not already had an invoice. | |
$invoices = Stripe\Invoice::all( array( 'customer' => $customer_id, 'limit' => 1 ) ); | |
if ( empty( $invoices['data'] ) ) { | |
$checkout_session_params['subscription_data']['trial_period_days'] = 7; | |
$checkout_session_params['payment_method_collection'] = 'always'; // Delete this line if you want users to be able to trial without entering payment method info. | |
} | |
} | |
return $checkout_session_params; | |
} | |
add_filter( 'rwstripe_checkout_session_params', 'rwstripetrial_checkout_session_params', 10, 3 ); | |
/** | |
* Show a 7 day trial in product price. | |
*/ | |
function rwstripetrial_format_price( $price_text, $price ) { | |
// Make sure this price is recurring and not a "choose your own price". | |
if ( empty( $price->custom_unit_amount ) && $price->recurring ) { | |
// Set up a default currency format. | |
$currency_format = array( | |
'decimals' => '2', | |
'thousands_separator' => ',', | |
'decimal_separator' => '.', | |
'symbol' => '$', | |
'position' => 'left', | |
); | |
// If Stripe Price has a different currency, check if we have a format for it. | |
if ( ! empty( $rwstripe_currency_variations[ strtoupper( $price->currency ) ] ) ) { | |
// We have a varaition for this currency. Merge it with the default. | |
$currency_format = array_merge( $currency_format, $rwstripe_currency_variations[ strtoupper( $price->currency ) ] ); | |
} | |
$format_number = number_format( | |
(float) $price->unit_amount / (float) pow( 10, $currency_format['decimals'] ), | |
$currency_format['decimals'], | |
$currency_format['decimal_separator'], | |
$currency_format['thousands_separator'] | |
); | |
ob_start(); | |
?> | |
<div style="text-align: center;"> | |
<p> | |
<span class="rwstripe-price-unit">7 days free</span> | |
</p> | |
<p> | |
<span class="rwstripe-price-per">then $<?php echo preg_replace( '/' . preg_quote( $currency_format['decimal_separator'], '/' ) . '0+$/', '', $format_number ); ?> per <?php if ( intval( $price->recurring->interval_count ) > 1 ) { echo $price->recurring->interval_count . ' '; } echo $price->recurring->interval; ?></span> | |
</p> | |
</div> | |
<?php | |
$price_text = ob_get_clean(); | |
} | |
return $price_text; | |
} | |
add_filter( 'rwstripe_format_price', 'rwstripetrial_format_price', 10, 2 ); | |
// If you need more control over how the "purchase" box displays, you can use the `rwstripe_restricted_content_message` filter. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment