uo_redirect_after_checkout
Filters the checkout redirect behavior, allowing modification of the redirect URL after a successful order.
add_filter( 'uo_redirect_after_checkout', $callback, 10, 2 );
Description
Filters the redirect URL after a WooCommerce checkout. Allows developers to customize the post-checkout redirection logic, for example, to send users to specific pages based on order contents or user roles. The order ID is provided to help determine the appropriate redirect.
Usage
add_filter( 'uo_redirect_after_checkout', 'your_function_name', 10, 2 );
Parameters
-
$continue(mixed) - This parameter is not used by the `uo_redirect_after_checkout` filter hook as provided in the source context.
-
$order_id(mixed) - This parameter is used to determine whether to continue with the default redirect behavior after checkout.
Return Value
The filtered value.
Examples
/**
* Example function to demonstrate the uo_redirect_after_checkout filter.
* This function checks if the order contains a specific product and, if so,
* modifies the redirect behavior.
*
* @param mixed $continue The original value of the continue flag (usually boolean).
* @param int $order_id The ID of the completed WooCommerce order.
* @return mixed The modified continue flag. If true, a redirect will occur.
*/
function my_custom_redirect_after_checkout( $continue, $order_id ) {
// Get the order object.
$order = wc_get_order( $order_id );
// Ensure the order exists.
if ( ! $order ) {
return $continue;
}
// Define the product ID you want to trigger a custom redirect for.
$specific_product_id = 123; // Replace with your actual product ID.
// Get order line items.
$line_items = $order->get_items( 'line_item' );
$found_specific_product = false;
foreach ( $line_items as $item ) {
// Check if the product in this line item matches our specific product ID.
if ( $item->get_product_id() === $specific_product_id ) {
$found_specific_product = true;
break; // No need to check further if we found it.
}
}
// If the specific product is found, we want to trigger a redirect.
// We'll set $continue to true to allow the original filter logic to handle the redirect.
if ( $found_specific_product ) {
// Optionally, you could add a custom meta to the order or user here
// to mark that this specific product was purchased.
// update_post_meta( $order_id, '_my_custom_redirect_triggered', true );
// Set continue to true to allow the redirect to proceed.
// The actual redirect URL is handled by the core function this filter is attached to.
return true;
}
// If the specific product was not found, return the original $continue value.
return $continue;
}
// Add the filter to WordPress.
// 'uo_redirect_after_checkout' is the hook name.
// 'my_custom_redirect_after_checkout' is the name of our callback function.
// 20 is the priority (determines the order in which filters are executed).
// 2 is the number of accepted arguments ($continue, $order_id).
add_filter( 'uo_redirect_after_checkout', 'my_custom_redirect_after_checkout', 20, 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.php:1132
public function woocommerce_redirect_to_group( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$user_id = $order->get_user_id();
$transient = maybe_unserialize( $order->get_meta( '_ulgm_user_' . $user_id . '_order', true ) );
$transient2 = maybe_unserialize( $order->get_meta( '_ulgm_user_buy_courses_' . $user_id . '_order', true ) );
if ( $transient || $transient2 ) {
return;
}
if ( ! $order->has_status( 'completed' ) ) {
return;
}
$line_items = $order->get_items( 'line_item' );
if ( ! $line_items ) {
return;
}
$continue = false; //Added to remove force redirect unless user overrides by following filter
$continue = apply_filters( 'uo_redirect_after_checkout', $continue, $order_id );
if ( true === $continue && ! is_admin() ) {
$url = SharedFunctions::get_group_management_page_id( true );
wp_safe_redirect( $url );
exit;
}
}