Redirection pour trois articles – Id des articles et url des pages destination spécifiés dans le code:
/**
* WOOCOMMERCE REDIRECT ON SPECIFIC PAGE AFTER PURCHASE BASED ON X SPECIFICS PRODUCTS (by ID of the product)
* By VPG
* version 1.0
* adapted from "LoicTheAztec" at
* https://wordpress.stackexchange.com/questions/336411/woocommerce-redirect-thankyou-page-based-on-product-id-doesnt-empty-cart
*/
add_action( 'template_redirect', 'custom_order_received_redirect' );
function custom_order_received_redirect(){
// Only on order received (thankyou) page for a valid Order ID
if( is_wc_endpoint_url('order-received')
&& ( $order_id = absint( get_query_var('order-received') ) )
&& get_post_type($order_id) === 'shop_order'
&& isset( $_GET['key'] ) ) {
// Empty cart
if ( ! WC()->cart->is_empty() ) {
WC()->cart->empty_cart();
}
// Get an instance of the WC_Order Object
$order = wc_get_order( $order_id );
// Check that the order key is valid
if( is_a($order, 'WC_Order') && $order->get_order_key() === esc_attr($_GET['key']) ) {
// HERE define your products IDs in the array
$product_ids1 = array(224);
$product_ids2 = array(221);
//$product_ids3 = array(77);
// HERE define your redirection URL (with the order ID as argument if needed)
$redirection_url1 = home_url("/commande-abonnement/");
$redirection_url2 = home_url("/commande-inscription/");
//$redirection_url3 = home_url("/troisieme-page/");
// Loop through order items
foreach( $order->get_items() as $item ) {
if( in_array( $item->get_product_id(), $product_ids1 ) ) {
wp_redirect( $redirection_url1 );
exit();
}
if( in_array( $item->get_product_id(), $product_ids2 ) ) {
wp_redirect( $redirection_url2 );
exit();
}
if( in_array( $item->get_product_id(), $product_ids3 ) ) {
wp_redirect( $redirection_url3 );
exit();
}
}
}
}
}
PARTICULARITÉ : Rediriger vers la page Author du user
ATTENTION !
Cela ne fonctionne que si WOOCOMMERCE CRÉE UN COMPTE LORS DE L’ACHAT.
IL faut donc Autoriser les clients à créer un compte au cours de la validation de commande et ne pas permettre qu’une commande soit passÉe sans compte

// Get the current user ID (nécessaire pour updater le champs user meta)
$user_id = $order->get_user_id();
// HERE define your redirection URL (with the order ID as argument if needed)
$redirection_url1 = home_url("/?author=" . $user_id . "/");
// Loop through order items
foreach( $order->get_items() as $item ) {
if( in_array( $item->get_product_id(), $product_ids1 ) ) {
wp_redirect( $redirection_url1 );
exit();
}
Solution reprenant cette idée dans ce plugin