How to Add Custom Expiration in WooCommerce Subscriptions

We are going to see today a very simple way to increase the expiration options of a subscription offered by WooCommerce Subscriptions.

We create a subscription-type product

When we create a subscription-type product we have to establish in its configuration if we want there to be an expiration for that, and if so, for how long it will be. WooCommerce Subscriptions only gives us the possibility to choose up to 24 months.

If we need to increase the options we can use this filter that we will place, as always, in the functions file.php of our child theme:

add_filter( ‘woocommerce_subscription_lengths’, ‘cmk_extend_subscription_expiration_options’);

function cmk_extend_subscription_expiration_options( $subscription_lengths ) {

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

return $subscription_lengths;

}

As can be seen, in this case I have added the option corresponding to 36 months of expiration, being able to add all the ones you need.

Add different options

In this way, if we want to add several more options , for example, those corresponding to 27, 28, 29 and 30 months we would have to use the following:

add_filter( ‘woocommerce_subscription_lengths’, ‘cmk_extend_subscription_expiration_options’);

function cmk_extend_subscription_expiration_options( $subscription_lengths ) {

$subscription_lengths[‘month’][27] = wcs_get_subscription_period_strings( 27, ‘month’);

$subscription_lengths[‘month’][28] = wcs_get_subscription_period_strings( 28, ‘month’);

$subscription_lengths[‘month’][29] = wcs_get_subscription_period_strings( 29, ‘month’);

$subscription_lengths[‘month’][30] = wcs_get_subscription_period_strings( 30, ‘month’);

}

As you can see, adding the personalized expiration in WooCommerce Subscriptions is very easy and helps us adapt to the needs of our online store.

Leave a Comment

Item added to cart.
0 items - 0,00