Skip to content

Instantly share code, notes, and snippets.

@ejntaylor
Last active July 7, 2024 08:03
Show Gist options
  • Save ejntaylor/9287875f4d0fff5499ae to your computer and use it in GitHub Desktop.
Save ejntaylor/9287875f4d0fff5499ae to your computer and use it in GitHub Desktop.
WooCommerce subscriptions without credit card
<?php
// Free Trial - Let GF Reg use email as username
/**
* WordPress register with email only, make it possible to register with email
* as username in a multisite installation
* @param Array $result Result array of the wpmu_validate_user_signup-function
* @return Array Altered result array
*/
function custom_register_with_email($result) {
if ( $result['user_name'] != '' && is_email( $result['user_name'] ) ) {
unset( $result['errors']->errors['user_name'] );
}
return $result;
}
add_filter('wpmu_validate_user_signup','custom_register_with_email');
// Free Trial - Auto login after reg
add_action( 'gform_user_registered', 'pi_gravity_registration_autologin', 10, 4 );
/**
* Auto login after registration.
*/
function pi_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
$user = get_userdata( $user_id );
$user_login = $user->user_login;
$user_password = $password;
wp_signon( array(
'user_login' => $user_login,
'user_password' => $user_password,
'remember' => false
) );
}
// Free Trial Over Hook
function grow_trial_redirect(){
global $user_ID;
$user = get_userdata( $user_ID );
if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
return;
}
// only apply to free trial users
if (!current_user_can( 'free_trial' ) || current_user_can( 'manage_options' )) {
return;
}
if( get_trial_days() > 15 && !is_front_page()){
wp_redirect(wc_grow_payment_page());
exit;
} else {
return;
}
}
add_action('template_redirect', 'grow_trial_redirect', 10, 3);
// get trial days left
function get_trial_days() {
global $user_ID;
$user = get_userdata( $user_ID );
$regtime = strtotime($user->user_registered);
$now = strtotime("now");
$diff = $now - $regtime;
$days = $diff / 60 / 60 / 24 ;
$hours = $diff / 60 / 60;
return round($days);
}
function get_trial_days_left() {
return 15 - get_trial_days();
}
// show message for trial period
function trial_message() {
if (!is_user_logged_in()) return;
global $user_ID;
$user = get_userdata( $user_ID );
$shopowner_allowed_roles = array('store_owner_free');
if( !array_intersect($shopowner_allowed_roles, $user->roles ) || is_checkout() || is_account_page()) return;
if (get_trial_days_left() <= 0) {
echo '<div class="wc-grow-trial-message">Trial Over <a href="'.wc_grow_payment_page().'">Purchase Now</a></div>';
} else {
echo '<div class="wc-grow-trial-message">You have '. get_trial_days_left() .' days left of your Trial. <a href="'.wc_grow_payment_page().'">Click here to upgrade.</a></div>';
}
}
add_action('wp_head','trial_message');
function wc_grow_payment_page() {
return '/checkout/?add-to-cart=4031';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment