Filter uncanny-learndash-groups

ulgm_create_license_redirect_target

Filters the target URL for license creation redirects after checkout, allowing modification based on product and class.

add_filter( 'ulgm_create_license_redirect_target', $callback, 10, 1 );

Description

Filters the redirect target URL for license creation. Developers can modify the target from 'checkout' to 'cart' based on product ID or class context. This allows for custom redirect behavior after a license is generated, defaulting to the checkout page.


Usage

add_filter( 'ulgm_create_license_redirect_target', 'your_function_name', 10, 1 );

Parameters

$product_id (mixed)
This parameter is the default target for the redirect when creating a license, which is 'checkout' by default.

Return Value

The filtered value.


Examples

/**
 * Example function to modify the redirect target for license creation.
 *
 * This function will change the redirect target to the cart page instead of
 * the checkout page when a specific product ID is being processed.
 *
 * @param mixed $redirect_target The current redirect target (e.g., 'checkout').
 * @param mixed $product_id      The ID of the product being processed.
 * @param mixed $class_name      The name of the class where the filter is applied.
 * @return string The modified redirect target.
 */
function my_custom_license_redirect_target( $redirect_target, $product_id, $class_name ) {
	// Define a specific product ID that should redirect to the cart.
	$special_product_id = 123; // Replace with your actual product ID.

	// Check if the current product ID matches our special product ID
	// and if the current redirect target is 'checkout'.
	if ( $product_id === $special_product_id && 'checkout' === $redirect_target ) {
		// If it matches, change the redirect target to 'cart'.
		return 'cart';
	}

	// Otherwise, return the original redirect target.
	return $redirect_target;
}
add_filter( 'ulgm_create_license_redirect_target', 'my_custom_license_redirect_target', 10, 3 );

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/helpers/shared-functions.php:775

public static function get_checkout_return_url( $product_id = 0, $qty = 1 ) {
		$suffix = "?add-to-cart={$product_id}&quantity={$qty}";
		if ( defined( 'EVENT_TICKETS_PLUS_FILE' ) ) {
			$url = wc_get_cart_url();
		} else {
			$url = wc_get_checkout_url();
			if ( 'cart' === apply_filters( 'ulgm_create_license_redirect_target', 'checkout', $product_id, __CLASS__ ) ) {
				$url = wc_get_cart_url();
			}
		}

		return "{$url}{$suffix}";
	}

Scroll to Top