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!
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
}
I believe the type is subscription_variation if its a variable product.
Cool
wc_get_product( $product_id )
I cannot get any result write this but when add numeric product id it works. How can I get product id programmatically?
It depends. On which page are you? In general, you should call:
and then you can get the ID and other properties via https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/