skiel (taylor's version)
skiel (taylor's version)
KPCKevin Powell - Community
Created by skiel (taylor's version) on 7/17/2023 in #back-end
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.
2 replies
KPCKevin Powell - Community
Created by skiel (taylor's version) on 5/17/2023 in #back-end
Resources for technical interview Javascript/NodeJs
Hey everyone! I luckily got an interview for a internship at a company. The position is for a NodeJs developer and I've been told the interview will evaluate my javascript and nodejs knowledge. Is there any resource you'd recommend so I can practice before? I'll gladly appreciate anything you can offer.
7 replies