WooCommerce Subscriptions custom shortcode for recurring totals

I need a shortcode to display the monthly recurring totals for a WooCommerce subscription order. After checkout, I'm requiring a customer to sign a lease for the rental order they just placed and need a way to show what their first monthly payment will be. Because I prorate, the order subtotals and totals don't contain the correct info. I tried using the following, based on the documentation here: https://woocommerce.com/document/subscriptions/develop/functions/ add_shortcode('wdm_recurring_totals', 'wmd_my_custom_function'); function wmd_my_custom_function(){ WC_Subscription::get_total(); } It seems to just throw an error on testing (I'm inexperienced with PHP).

Comment (1)

Jese Leos

October 25, 2024

Verified user

You can achieve this by creating a custom shortcode that calculates the monthly recurring total for a WooCommerce subscription. Here’s how you can do it: add_shortcode('wdm_recurring_totals', 'wmd_my_custom_function'); function wmd_my_custom_function() { // Check if the user is logged in if ( ! is_user_logged_in() ) { return 'You need to be logged in to view this information.'; } // Get the current user's subscriptions $user_id = get_current_user_id(); $subscriptions = wcs_get_users_subscriptions( $user_id ); // Initialize total $total = 0; // Loop through each subscription foreach ( $subscriptions as $subscription ) { // Check if the subscription is active if ( $subscription->get_status() == 'active' ) { // Get the monthly recurring total $total += $subscription->get_total(); } } // Check if total is zero if ( $total === 0 ) { return 'You have no active subscriptions.'; } // Return the formatted total return 'Your first monthly payment will be: ' . wc_price( $total ); }

You’ll be in good company