Subscription Product Function Reference

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 ↑ Back to top

Returns a string representing the details of the subscription. For example “$20 per Month for 3 Months with a $10 sign-up fee”.

Usage ↑ Back to top

<?php WC_Subscriptions_Product::get_price_string( $product ) ?>

Parameters ↑ Back to top

$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 ↑ Back to top

(string)
This string representation of a subscription price including period, duration and sign-up fee, if set.

WC_Subscriptions_Product::get_price ↑ Back to top

Returns the price per period for a product if it is a subscription.

Usage ↑ Back to top

<?php WC_Subscriptions_Product::get_price( $product ) ?>

Parameters ↑ Back to top

$product
(mixed) (required) A WC_Product object or product ID.Default: None

Return Values ↑ Back to top

(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 ↑ Back to top

Returns the subscription period for a product, if it’s a subscription.

Usage ↑ Back to top

<?php WC_Subscriptions_Product::get_period( $product ) ?>

Parameters ↑ Back to top

$product
(mixed) (required) A WC_Product object or product ID.Default: None

Return Values ↑ Back to top

(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 ↑ Back to top

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 ↑ Back to top

<?php WC_Subscriptions_Product::get_length( $product ) ?>

Parameters ↑ Back to top

$product
(mixed) (required) A WC_Product object or product ID.Default: None

Return Values ↑ Back to top

(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 ↑ Back to top

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 ↑ Back to top

<?php WC_Subscriptions_Product::get_expiration_date( $product_id, $order_date ) ?>

Parameters ↑ Back to top

$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 ↑ Back to top

(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.

WooCommerce - the most customizable eCommerce platform for building your online business.

Back to the top