Get WooCommerce Details From WooCommerce Subscription Object Or Subscription ID
Once you have the WooCommerce Subscription Object (WC_Subscription) or Subscription ID, you can fetch any data relevant to the subscription easily using PHP.
For this tutorial you will be adding PHP to the functions.php file.
If you are not sure how to access functions.php you should read our article on how to edit functions.php in WordPress.
How To Get The WooCommerce Subscriptions Object (WC_Subscription)
Depending on where you’re editing the code, you will have access to either the subscription_id or the subscription object, or neither, the below will assist in retrieving the subscription.
If you’re not sure -> drop a comment at the bottom of this article and we’ll assist you.
// Assuming you have a WooCommerce Subscription ID
$subscription = wcs_get_subscription( $subscription_id );
// If you don't have the subscription_id, you can try searching the global if you are on a subscription page
global $post;
$subscription_id = $post->ID
$subscription = wcs_get_subscription( $subscription_id );
// Otherwise, assuming you have the subscription object, it's wise to make sure.
// If you try call a method on the subscription object and it's not set, it wll trigger a fatal error
// This will stop the function from continuing to run if this is not a subscription
if ( ! is_a( $subscription, 'WC_Subscription' ) ) return false;
Get Subscription Details From The WooCommerce Subscriptions Object
Assuming that you have correctly fetched the WC_Subscription object, these are the following available methods that will be useful for getting subscription data:
P.S. You can find the original developer docs relevant to the below here for WC Subscription Methods.
/**
*
* Available Methods from WC_Subscriptions object
* Extends WC_Order, so any method within WC_Order is also available in WC_Subscription
* @see https://woocommerce.com/document/subscriptions/develop/functions/
*
* @url https://wpdavies.dev/how-to-get-woocommerce-subscription-info/
* @author Christopher Davies, WP Davies
*
**/
// Subscription Meta
$subscription->get_id(); // Subscription ID
$subscription->get_parent_id(); // Order ID of the original order when subscription was placed
$subscription->get_type();
$subscription->get_status();
$subscription->get_date( string $date, string $timezone ); // This is a useful function for fetching dates, Accepts: 'start', 'trial_end', 'next_payment', 'last_payment' or 'end'
$subscription->get_date_paid();
$subscription->get_date_completed();
$subscription->get_date_created();
$subscription->get_date_modified();
$subscription->get_currency();
$subscription->get_created_via();
$subscription->get_customer_note();
$subscription->get_recorded_sales();
$subscription->get_customer_order_notes();
$subscription->get_items();
$subscription->get_item_count();
$subscription->get_download_url();
$subscription->get_downloadable_items();
$subscription->get_shipping_methods();
// URLs
$subscription->get_checkout_payment_url();
$subscription->get_checkout_order_received_url();
$subscription->get_cancel_order_url();
$subscription->get_cancel_order_url_raw();
$subscription->get_cancel_endpoint();
$subscription->get_edit_order_url();
// Subscription Payment Info
$subscription->get_billing_period();
$subscription->get_billing_interval();
$subscription->get_trial_period();
$subscription->get_payment_count();
$subscription->get_failed_payment_count();
$subscription->get_total_initial_payment();
$subscription->get_suspension_count();
$subscription->get_requires_manual_renewal();
$subscription->get_switch_data();
$subscription->get_sign_up_fee();
$subscription->get_payment_method();
$subscription->get_payment_method_title();
// Subscription Helper Functions
$subscription->get_related_orders();
$subscription->get_last_order();
$subscription->get_payment_method_to_display();
$subscription->get_view_order_url();
$subscription->get_items_sign_up_fee();
$subscription->get_item_downloads();
$subscription->get_change_payment_method_url();
$subscription->get_payment_method_meta();
$subscription->get_completed_payment_count();
// Customer Data
$subscription->get_customer_id();
$subscription->get_user_id();
$subscription->get_user();
$subscription->get_billing_first_name();
$subscription->get_billing_last_name();
$subscription->get_billing_company();
$subscription->get_billing_address_1();
$subscription->get_billing_address_2();
$subscription->get_billing_city();
$subscription->get_billing_state();
$subscription->get_billing_postcode();
$subscription->get_billing_country();
$subscription->get_billing_email();
$subscription->get_billing_phone();
$subscription->get_shipping_first_name();
$subscription->get_shipping_last_name();
$subscription->get_shipping_company();
$subscription->get_shipping_address_1();
$subscription->get_shipping_address_2();
$subscription->get_shipping_city();
$subscription->get_shipping_state();
$subscription->get_shipping_postcode();
$subscription->get_shipping_country();
$subscription->get_shipping_phone();
$subscription->get_customer_ip_address();
$subscription->get_customer_user_agent();
$subscription->get_shipping_address_map_url();
$subscription->get_formatted_billing_full_name();
$subscription->get_formatted_shipping_full_name();
$subscription->get_formatted_billing_address();
$subscription->get_formatted_shipping_address();
// Sales Data
$subscription->get_total();
$subscription->get_total_tax();
$subscription->get_total_discount();
$subscription->get_subtotal();
$subscription->get_tax_totals();
$subscription->get_discount_total();
$subscription->get_discount_tax();
$subscription->get_shipping_total();
$subscription->get_shipping_tax();
$subscription->get_fees();
$subscription->get_total_fees();
$subscription->get_taxes();
// Refund Data
$subscription->get_total_tax_refunded();
$subscription->get_total_shipping_refunded();
$subscription->get_item_count_refunded();
$subscription->get_total_qty_refunded();
$subscription->get_refunds();
$subscription->get_total_refunded();
$subscription->get_qty_refunded_for_item();
$subscription->get_total_refunded_for_item();
$subscription->get_tax_refunded_for_item();
// Coupon Data
$subscription->get_coupon_codes();
$subscription->get_recorded_coupon_usage_counts();
$subscription->get_coupons();
$subscription->get_used_coupons();
how to get subscription info by user_id?
For example subscription ID?
I have a function that get user id:
$user_id = get_current_user_id();
Then?
Hi Antonio, You would need to use the wcs_get_subscriptions function. Something like the below would search for all subscriptions for a user_id and then loop through each: // Your customer id that you’re looking for $user_id = get_current_user_id(); // Fetch subscriptions belonging to this customer ID $subscriptions = wcs_get_subscriptions( array( ‘subscriptions_per_page’ => –1, // Fetch all subscriptions ‘customer_id’ => $user_id // Search by Customer / User ID ) ); … Read more »