Redirect WordPress website

Hey guys, I'm kinda in a selection process and I have to do some tasks. One of them is to redirect when a checkout happens on a WooCommerce. They gave me the code that would do it and instructions. Problem is: I don't know PHP, I came with a few ways to do it but don't know if it would lead to any problem as I don't know how PHP code works. This is the code they sent me:
add_action( 'template_redirect', 'conditional_redirection_after_payment');
function conditional_redirection_after_payment(){
// When "thankyou" order-received page is reached …
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;

// Get the order ID from the browser url
$order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// If the order status is 'failed' we stop the function
if( $order->has_status( 'failed' ) ) return;

// HERE set in the array for each product ID the corresponding url final path
$product_id_url_paths = array(
'691' => '/card-reading',
);
// Iterating through each order items

foreach( $order->get_items() as $item_id => $item_values ){
// The Product ID

$product_id = $item_values->get_product_id();
foreach( $product_id_url_paths as $key_id => $url_path ){
if( $key_id == $product_id ){

// Product is found and ID match: we got our path url. We redirect
wp_redirect( home_url( $url_path ) );
exit(); // always exit
}
}
}
}
}
add_action( 'template_redirect', 'conditional_redirection_after_payment');
function conditional_redirection_after_payment(){
// When "thankyou" order-received page is reached …
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;

// Get the order ID from the browser url
$order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// If the order status is 'failed' we stop the function
if( $order->has_status( 'failed' ) ) return;

// HERE set in the array for each product ID the corresponding url final path
$product_id_url_paths = array(
'691' => '/card-reading',
);
// Iterating through each order items

foreach( $order->get_items() as $item_id => $item_values ){
// The Product ID

$product_id = $item_values->get_product_id();
foreach( $product_id_url_paths as $key_id => $url_path ){
if( $key_id == $product_id ){

// Product is found and ID match: we got our path url. We redirect
wp_redirect( home_url( $url_path ) );
exit(); // always exit
}
}
}
}
}
I already changed the product ID, I read that I have to put the requested URL to redirect in the "wp_redirect("url")" but I don't know if doing just that would lead to any problems with the rest of the code. I'm sure it will. Please help.
1 Reply
Skiel
Skiel16mo ago
Im guessing the capital HERE means something, not only it is the place where I change the product ID but I'm guessing where I have to tweak the URL. To clarify: "/card-reading" is the example path that I was given, the URL I need to redirect doesn't have a path, is just the plain URL of bbc.
Want results from more Discord servers?
Add your server