1. Home
  2. Docs
  3. Getting Started
  4. Snippets

Snippets

Note: as of Version 1.3.8.1 you can go to Settings > Payment Plans and add your own custom HTML text by using the shortcodes: [today_subtotal], [interval], [period], [length], [payment_count], [grand_subtotal] and [sign_up_price].

There are two hooks available that you can use to customize the cart item price and the cart item subtotal on the Cart page.

Both snippets can be added to the bottom of your functions.php file in your child theme or using the Code Snippets plugin.

   IN THIS ARTICLE

LearnDash Compatibility

Will a user’s access to a course expire after all payment plans are paid?

A common issue that occurs with LearnDash courses is that, when a user completes all installments on a payment plan, their subscription status is updated from Active to Expired. So, LearnDash then removes access to the course for the user.

To allow a user to continue to have access to their LearnDash course after making all payments, add the following snippet to your functions.php file in your child theme.

 

function my_wpp_keep_course_access_on_expired_subscriptions(){ 	
	remove_action('woocommerce_subscription_status_expired', array('Learndash_WooCommerce', 'remove_subscription_course_access')); 
}  

add_action('init', 'my_wpp_keep_course_access_on_expired_subscriptions');

 

Note: as of LearnDash LMS – WooCommerce Integration plugin version 1.9.1 you DO NOT need to add this snippet! Instead, on your WordPress dashboard, go to WooCommerce Settings > and click the Subscriptions tab. Next, scroll down to the bottom of the page and check the Disable checkbox  under “Access Removal on Expiration”. Now, course access will NOT be revoked when a subscription expires. For reference, see the screenshot below.

 

How can I extend more than 24 months financing to 36, 48, 60, 72, and 84 months?

Add the following snippet to your functions.php file or using the Code Snippets plugin:

// Extend Payment Plan lengths for 36 months, 48 months, 60 months, 72 months, 84 months
function wpp_extend_subscription_expiration_options( $subscription_lengths ) {

	$subscription_lengths['month'][36] = wcs_get_subscription_period_strings(36, 'month');

	$subscription_lengths['month'][48] = wcs_get_subscription_period_strings(48, 'month');

	$subscription_lengths['month'][60] = wcs_get_subscription_period_strings(60, 'month');

	$subscription_lengths['month'][72] = wcs_get_subscription_period_strings(72, 'month');

	$subscription_lengths['month'][84] = wcs_get_subscription_period_strings(84, 'month');

	return $subscription_lengths;
}

add_filter('woocommerce_subscription_lengths', 'wpp_extend_subscription_expiration_options');

After adding this snippet you will have the option to extend payment plan length for 36 months, 48 months, 60 months, 72 months, 84 months.

Extend Payment Plan intervals

Add the following snippet to your functions.php file or using the Code Snippets plugin:

function wpp_extend_subscription_period_intervals( $intervals ) {

	// Set custom intervals for WooCommerce Subscriptions. The max default interval is 6

	$intervals[7] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 7 ) );

	$intervals[8] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 8 ) );

	$intervals[9] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 9 ) );

	$intervals[10] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 10 ) );

	$intervals[11] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 11 ) );

	$intervals[12] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 12 ) );

	$intervals[24] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 24 ) );

	return $intervals;
}
add_filter( 'woocommerce_subscription_period_interval_strings', 'wpp_extend_subscription_period_intervals' );

After adding this snippet you will have the option to extend payment plan intervals.

For example, after adding the snippet above the interval options are updated when adding a payment plan:

Price and Subtotal filters

Product price filter

By default, the product price on the Cart page shows the total product price for all periods.
So, if a pair of sunglasses costs $100, the total product price shows as $100 even though the customer is paying $25 every month for 4 periods.

Example #1: Product price for one period
function my_wpp_cart_item_price($price_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #1
	// <span class='price-amount'>$XX</span> (price for one period, eg. one month)
	$price_html = wc_price($product_price);

	return $price_html;
}

add_filter('wpp_cart_item_price_html', 'my_wpp_cart_item_price', 10, 8);

Example #2: Product price for all periods
function my_wpp_cart_item_price($price_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #2
	// or price for all periods:
	$price_html = wc_price($product_price * $scheme_length / $scheme_interval);

	return $price_html;
}

add_filter('wpp_cart_item_price_html', 'my_wpp_cart_item_price', 10, 8);

Example #3: Product price (all periods + sign-up fee)
function my_wpp_cart_item_price($price_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #3
	// or full price (all periods + sign-up fee):
	$price_html = wc_price($product_price * $scheme_length / $scheme_interval + $scheme_sign_up_price);

	return $price_html;
}

add_filter('wpp_cart_item_price_html', 'my_wpp_cart_item_price', 10, 8);

Note: no screenshot is shown for example #3 as the sunglasses product in this example does not have a sign-up fee.

Subtotal filter

By default, the product subtotal on the Cart page shows the price for one period + sign-up fee. In addition, it shows the price for all periods with the total amount payable over X periods.

Example #1: Product subtotal for one period

function my_wpp_cart_item_subtotal($subtotal_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #1
	// we can change it to smh like:
	// <span class='price-amount'>$XX</span> (price for one period, eg. one month)
	$subtotal_html = wc_price($product_price * $cart_item['quantity']);

	return $subtotal_html;
}

Example #2: Product subtotal for all periods

function my_wpp_cart_item_subtotal($subtotal_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #2
	// or price for all periods:
	$subtotal_html = wc_price($product_price * $cart_item['quantity'] * $scheme_length / $scheme_interval);

	return $subtotal_html;
}

Example #3: Product subtotal (all periods + sign-up fee)

function my_wpp_cart_item_subtotal($subtotal_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #3
	// or full price (all periods + sign-up fee):
	$subtotal_html = wc_price($product_price * $cart_item['quantity'] * $scheme_length / $scheme_interval + $scheme_sign_up_price * $cart_item['quantity']);

	return $subtotal_html;
}

Note: no screenshot is shown for example #3 as the Sunglasses product in this example does not have a sign-up fee.

 

Example #4: Product subtotal (all periods + sign-up fee) and a notice about the amount of payments

function my_wpp_cart_item_subtotal($subtotal_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #4
	// or full price (all periods + sign-up fee)
	// and a notice about the amount of payments (eg. 4 total payments)
	$subtotal_html = wc_price($product_price * $cart_item['quantity'] * $scheme_length / $scheme_interval + $scheme_sign_up_price * $cart_item['quantity']).
		"<div>". sprintf(_n('%s total payment', '%s total payments', $scheme_length / $scheme_interval, 'my-text-domain'), $scheme_length / $scheme_interval) ."</div>";

	return $subtotal_html;
}

add_filter('wpp_cart_item_subtotal_html', 'my_wpp_cart_item_subtotal', 10, 8);

Example #5: Product subtotal for one period and a notice about the amount of payments

function my_wpp_cart_item_subtotal($subtotal_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #5
	// price for one period
	// and a notice about the amount of payments (eg. 4 monthly payments)
	$subtotal_html = wc_price($product_price * $cart_item['quantity']).
		"<div>". sprintf(_n('%s monthly payment', '%s monthly payments', $scheme_length / $scheme_interval, 'my-text-domain'), $scheme_length / $scheme_interval) ."</div>";

	return $subtotal_html;
}

add_filter('wpp_cart_item_subtotal_html', 'my_wpp_cart_item_subtotal', 10, 8);

Example #6: Product subtotal for one period and a sign-up fee

function my_wpp_cart_item_subtotal($subtotal_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #6
	// total price today (price for one period + sign-up fee):
	$subtotal_html = wc_price($product_price * $cart_item['quantity'] + $scheme_sign_up_price * $cart_item['quantity']);

	return $subtotal_html;
}

add_filter('wpp_cart_item_subtotal_html', 'my_wpp_cart_item_subtotal', 10, 8);

Note: no screenshot is shown for example #6 as the Sunglasses product in this example does not have a sign-up fee.

 

By default both filters are applied only on the Cart page.

Product price filter

By default, the product price on the Cart page shows the total product price for all periods.
So, if a pair of sunglasses costs $100, the total product price shows as $100 even though the customer is paying $25 every month for 4 periods.

Example #1: Product price for one period
function my_wpp_cart_item_price($price_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #1
	// <span class='price-amount'>$XX</span> (price for one period, eg. one month)
	$price_html = wc_price($product_price);

	return $price_html;
}

add_filter('wpp_cart_item_price_html', 'my_wpp_cart_item_price', 10, 8);

Example #2: Product price for all periods
function my_wpp_cart_item_price($price_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #2
	// or price for all periods:
	$price_html = wc_price($product_price * $scheme_length / $scheme_interval);

	return $price_html;
}

add_filter('wpp_cart_item_price_html', 'my_wpp_cart_item_price', 10, 8);

Example #3: Product price (all periods + sign-up fee)
function my_wpp_cart_item_price($price_html, $cart_item, $cart_item_key, $product_price, $scheme_interval, $scheme_period, $scheme_length, $scheme_sign_up_price){

	// example #3
	// or full price (all periods + sign-up fee):
	$price_html = wc_price($product_price * $scheme_length / $scheme_interval + $scheme_sign_up_price);

	return $price_html;
}

add_filter('wpp_cart_item_price_html', 'my_wpp_cart_item_price', 10, 8);

Note: no screenshot is shown for example #3 as the sunglasses product in this example does not have a sign-up fee.

How can we help?