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.
This Subscription Product Function Reference guide is 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.
For a guide to creating and managing subscription products in a WooCommerce store, please refer to the Store Manager Guide instead.
Note: We are unable to provide support for customizations under our Support Policy. If you need to customize a snippet or extend its functionality, we recommend working with a Woo Agency Partner or finding a WooCommerce developer on Codeable.
WC_Subscriptions_Product::get_price_string
↑ Back to topReturns 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 topReturns 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 topReturns 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 topReturns 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 topTakes 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.