Home » WooCommerce: Check If Product Is A Subscription

WooCommerce: Check If Product Is A Subscription

by Tutor Aspire

We’ve already found out how to check if a WooCommerce product is simple, variable, grouped… Today we add more conditional tags as we study which WooCommerce “product types” are included within the official WooCommerce Subscriptions extension.

There are two new product types in such case: “Simple Subscription” and “Variable Subscription”, with the difference being you can offer multiple billing periods within the same product page (choice between daily, monthly and yearly for example) with the latter.

So, how do we know if a given product ID is a subscription, and also whether it’s a simple or variable one? Here’s the quick solution – enjoy!

You can create a “simple subscription” or a “variable subscription” product thanks to WooCommerce Subscriptions plugin. But how do we know if the product is a subscription from the frontend?

PHP: check if product ID is a subscription

$product = wc_get_product( $product_id );
if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
   // do something
}

PHP: check if product ID is a Simple subscription

$product = wc_get_product( $product_id );
if ( $product->is_type( 'subscription' ) ) {
   // do something
}

PHP: check if product ID is a Variable subscription

$product = wc_get_product( $product_id );
if ( $product->is_type( 'variable-subscription' ) ) {
   // do something
}

You may also like