This documentation is written for WooCommerce developers who want to extend or integrate with the WooCommerce Subscriptions plugin. To follow this documentation, you will need an advanced understanding of PHP and WordPress development.
If you are looking for a guide to creating and managing subscription products in a WooCommerce store, please refer to the Store Manager Guide instead. If you need to extend WooCommerce Subscriptions and are not a developer, you can find a developer in the WooExperts directory.
The WC_Subscriptions_Product
class is the Subscription extensions counterpart to the WC_Product
class. It is used to extend the WC_Product
class provide an API for accessing details of a subscription product type.
WC_Subscriptions_Product::get_price_string
Returns a string representing the details of the subscription. For example “$20 per Month for 3 Months with a $10 sign-up fee”.
Usage
<?php WC_Subscriptions_Product::get_price_string( $product ) ?>
Parameters
- $product
- (mixed) (required) A
WC_Product
object or product ID which will be checked to see if it is of the subscription type.Default: None
Return Values
- (string)
- This string representation of a subscription price including period, duration and sign-up fee, if set.
WC_Subscriptions_Product::get_price
Returns the price per period for a product if it is a subscription.
Usage
<?php WC_Subscriptions_Product::get_price( $product ) ?>
Parameters
- $product
- (mixed) (required) A
WC_Product
object or product ID.Default: None
Return Values
- (mixed)
- The price charged per period for the subscription, or an empty string if the product is not a subscription.
WC_Subscriptions_Product::get_period
Returns the subscription period for a product, if it’s a subscription.
Usage
<?php WC_Subscriptions_Product::get_period( $product ) ?>
Parameters
- $product
- (mixed) (required) A
WC_Product
object or product ID.Default: None
Return Values
- (string)
- A string representation of the period, either Day, Week, Month or Year, or an empty string if product is not a subscription.
WC_Subscriptions_Product::get_length
Returns the length of the subscription for a product, if it is a subscription.
Length is represented by an integer for the number of periods payment will recur.
For example, for a subscription lasting 6 months, this function would return 6.
Usage
<?php WC_Subscriptions_Product::get_length( $product ) ?>
Parameters
- $product
- (mixed) (required) A
WC_Product
object or product ID.Default: None
Return Values
- (int)
- An integer representing the length of the subscription, or 0 if the product is not a subscription or the subscription continues for perpetuity.
WC_Subscriptions_Product::get_expiration_date
Takes a subscription product’s ID and returns the date on which the subscription product will expire, based on the subscription’s length and calculated from either the $order_date
if specified, or the current date/time.
Usage
<?php WC_Subscriptions_Product::get_expiration_date( $product_id, $order_date ) ?>
Parameters
- $product
- (int) (required) The ID of the product to determine the expiration date for.Default: None
- $order_date
- (string) (optional) A MYSQL formatted date/time string from which to calculate the expiration date. Defaults to use today’s date/time.Default: Empty (today’s date)
Return Values
- (mixed)
- If the product has a duration, the return is the MYSQL formatted date/time of its expiration from
$order_date
. If no expiration is set, the return is 0.