Filter uncanny-learndash-groups

wcs_get_view_subscription_url

Filters the URL used to view a specific WooCommerce Subscriptions subscription.

add_filter( 'wcs_get_view_subscription_url', $callback, 10, 2 );

Description

Filters the URL for viewing a WooCommerce subscription in the account. Allows developers to modify the subscription view URL or redirect users to a different page, useful for custom subscription management or integration with other systems. The hook receives the original URL and the subscription ID.


Usage

add_filter( 'wcs_get_view_subscription_url', 'your_function_name', 10, 2 );

Parameters

$view_subscription_url (mixed)
This parameter holds the URL that will be used to display the subscription details in the WooCommerce My Account area, and it can be filtered to modify this URL.
$subscription_id (mixed)
This parameter is the URL that will be used to view the subscription.

Return Value

The filtered value.


Examples

/**
 * Filter the 'View Subscription' URL to point to a custom subscription management page.
 *
 * This function intercepts the default URL generated for viewing a subscription
 * in the WooCommerce My Account section and redirects it to a custom page,
 * potentially for enhanced subscription management features or integrations.
 *
 * @param string $view_subscription_url The default URL for viewing the subscription.
 * @param int    $subscription_id       The ID of the subscription.
 * @return string The modified or original URL for viewing the subscription.
 */
function custom_wcs_view_subscription_url_redirect( $view_subscription_url, $subscription_id ) {
	// Check if the subscription has a specific meta key indicating it should use a custom URL.
	// Replace 'custom_subscription_management_page_id' with your actual meta key.
	$custom_page_id = get_post_meta( $subscription_id, 'custom_subscription_management_page_id', true );

	if ( ! empty( $custom_page_id ) ) {
		// Construct the new URL pointing to your custom page.
		// Replace 'my-custom-subscription-page' with the actual slug of your custom page.
		$custom_page_permalink = get_permalink( $custom_page_id );
		// Append the subscription ID as a query parameter for the custom page to use.
		$view_subscription_url = add_query_arg( 'subscription_id', $subscription_id, $custom_page_permalink );
	}

	return $view_subscription_url;
}
add_filter( 'wcs_get_view_subscription_url', 'custom_wcs_view_subscription_url_redirect', 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/woocommerce/woocommerce-license-subscription.php:990

public function my_account_subscription_lines( WC_Subscription $subscription ) {
		$group_id = self::is_subscription_linked_to_a_group( $subscription );
		if ( false === $group_id ) {
			return;
		}
		$group_management_link = ulgm()->group_management->pages->get_group_management_page_id( true );
		$group_management_link = "$group_management_link?group-id=" . $group_id;
		?>
		<tr>
			<td><?php esc_html_e( 'Linked group', 'uncanny-learndash-groups' ); ?></td>
			<td>
				<span data-is_manual="<?php echo esc_attr( wc_bool_to_string( $subscription->is_manual() ) ); ?>">
					<a href="<?php echo esc_url_raw( $group_management_link ); ?>"><?php echo get_the_title( $group_id ); ?></a>
				</span>
			</td>
		</tr>
		<?php
		$group_id = get_post_meta( $subscription->get_id(), 'subscription_additional_seats_group_id', true );
		if ( empty( $group_id ) ) {
			return;
		}
		$main_subscription_id = get_post_meta( $group_id, '_group_main_subscription_id', true );
		$resubscription_ids   = get_post_meta( $group_id, '_group_resubscription_id' );
		if ( empty( $main_subscription_id ) && empty( $resubscription_ids ) ) {
			return;
		}
		if ( ! empty( $main_subscription_id ) ) {
			$subscription_id = $main_subscription_id;
		}

		if ( ! empty( $resubscription_ids ) && is_array( $resubscription_ids ) ) {
			$subscription_id = array_pop( $resubscription_ids );
		}

		if ( empty( $subscription_id ) ) {
			return;
		}
		$view_subscription_url = wc_get_endpoint_url( 'view-subscription', $subscription_id, wc_get_page_permalink( 'myaccount' ) );

		$view_subscription_url = apply_filters( 'wcs_get_view_subscription_url', $view_subscription_url, $subscription_id );
		?>
		<tr>
			<td><?php esc_html_e( 'Parent subscription', 'uncanny-learndash-groups' ); ?></td>
			<td>
				<span data-is_manual="<?php echo esc_attr( wc_bool_to_string( $subscription->is_manual() ) ); ?>">
					<a href="<?php echo esc_url_raw( $view_subscription_url ); ?>">#<?php echo esc_attr( $subscription_id ); ?></a>
				</span>
			</td>
		</tr>
		<?php
	}

Scroll to Top