Created
February 7, 2025 09:54
-
-
Save abdullahwp/c598de071c60dd5639c0c240190174b9 to your computer and use it in GitHub Desktop.
Shortcode to display WooCommerce Subscription user details on the user Account page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Shortcode to display WooCommerce Subscription details on the My Account page. | |
| * | |
| * Usage examples: | |
| * [woo_subscription_details] | |
| * [woo_subscription_details subscription_id="123"] | |
| * | |
| * - If no subscription_id is provided, the shortcode automatically fetches the current user's subscriptions. | |
| * * If the user has exactly one subscription, its details are shown. | |
| * * If the user has multiple subscriptions, the subscriptions list table is displayed. | |
| */ | |
| function woo_subscription_details_shortcode( $atts ) { | |
| // Ensure the user is logged in. | |
| if ( ! is_user_logged_in() ) { | |
| return esc_html__( 'Please log in to view your subscriptions.', 'textdomain' ); | |
| } | |
| ob_start(); | |
| $user_id = get_current_user_id(); | |
| // Get the current user's subscriptions as an array of WC_Subscription objects keyed by subscription ID. | |
| $customer_subscriptions = wcs_get_users_subscriptions( $user_id ); | |
| if ( empty( $customer_subscriptions ) ) { | |
| echo '<p>' . esc_html__( 'You have no active subscriptions.', 'textdomain' ) . '</p>'; | |
| return ob_get_clean(); | |
| } | |
| // Allow an override via shortcode attribute, but if not set, auto-detect. | |
| $atts = shortcode_atts( array( | |
| 'subscription_id' => '', | |
| ), $atts, 'woo_subscription_details' ); | |
| // If a subscription_id attribute is passed, use it. | |
| if ( ! empty( $atts['subscription_id'] ) ) { | |
| $subscription_id = absint( $atts['subscription_id'] ); | |
| if ( ! isset( $customer_subscriptions[ $subscription_id ] ) ) { | |
| echo '<p>' . esc_html__( 'Subscription not found or you do not have permission to view it.', 'textdomain' ) . '</p>'; | |
| return ob_get_clean(); | |
| } | |
| $subscription = $customer_subscriptions[ $subscription_id ]; | |
| } | |
| // Otherwise, if only one subscription exists, auto-select it. | |
| elseif ( count( $customer_subscriptions ) === 1 ) { | |
| $subscription = current( $customer_subscriptions ); | |
| } | |
| // If the user has multiple subscriptions, show the subscriptions list. | |
| else { | |
| // Determine current pagination page (if needed). | |
| $current_page = isset( $_GET['subscriptions-page'] ) ? absint( $_GET['subscriptions-page'] ) : 1; | |
| if ( function_exists( 'WCS_Template_Loader' ) ) { | |
| WCS_Template_Loader::get_my_subscriptions( $current_page ); | |
| } else { | |
| echo '<p>' . esc_html__( 'Subscriptions feature is not available.', 'textdomain' ) . '</p>'; | |
| } | |
| return ob_get_clean(); | |
| } | |
| // Display the details for the selected subscription. | |
| // Print any WooCommerce notices. | |
| wc_print_notices(); | |
| /** | |
| * These actions output the subscription details table and totals. | |
| * They rely on the WooCommerce Subscriptions plugin templates. | |
| */ | |
| do_action( 'woocommerce_subscription_details_table', $subscription ); | |
| do_action( 'woocommerce_subscription_totals_table', $subscription ); | |
| do_action( 'woocommerce_subscription_details_after_subscription_table', $subscription ); | |
| // Optionally, display additional order details. | |
| wc_get_template( 'order/order-details-customer.php', array( 'order' => $subscription ) ); | |
| return ob_get_clean(); | |
| } | |
| add_shortcode( 'woo_subscription_details', 'woo_subscription_details_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment