Home / Admin / Subscription Status Shortcode
Duplicate Snippet

Embed Snippet on Your Site

Subscription Status Shortcode

Adds a shortcode to display a short subscription status description for recurring payments.

Code Preview
php
<?php
/**
 * Add a shortcode which prints a small status description for
 * subscriptions that are active through Easy Digital Downloads Recurring
 * Payments platform.
 */
if ( !function_exists( 'eddsss_subscription_status_shortcode' ) ) {
function eddsss_subscription_status_shortcode() {
	if ( !is_user_logged_in() || !class_exists( 'EDD_Recurring_Customer' ) ) {
		return;
	}
	$user_id = get_current_user_id();
	$status = EDD_Recurring_Customer::get_customer_status( $user_id );
	if ( $status !== 'active' && $status !== 'cancelled' && $status !== 'expired' ) {
		return;
	}
	// Check if expired and set to expired if so
	if( EDD_Recurring_Customer::is_customer_expired( $user_id ) ) {
		$status = 'expired';
		EDD_Recurring_Customer::set_customer_status( $user_id, $status );
	}
	$message = '';
	switch( $status ) {
		
		case 'active' :
			$message = sprintf( __( 'Your subscription is active and will renew on %s.', 'edd' ), date_i18n( get_option( 'date_format' ), EDD_Recurring_Customer::get_customer_expiration( $user_id ) ) );
			break;
			
		case 'cancelled' :
			$message = sprintf( __( 'Your subscription is cancelled and expires on %s.', 'edd' ), date_i18n( get_option( 'date_format' ), EDD_Recurring_Customer::get_customer_expiration( $user_id ) ) );
			break;
		case 'expired' :
			$message = sprintf( __( 'Your subscription expired on %s.', 'edd' ), date_i18n( get_option( 'date_format' ), EDD_Recurring_Customer::get_customer_expiration( $user_id ) ) );
			break;
	}
	if ( !empty( $message ) ) {
		return '<div id="edd-subscription-status" class="' . esc_attr( $status ) . '">' . $message . '</div>';
	}
}
add_shortcode( 'edd_subscription_status', 'eddsss_subscription_status_shortcode' );
} // endif;

Comments

Add a Comment