Cothurnes : solution ultime – redirection après achat vers page auteur – attribution de rôle – inscriptiuon du statut du user dans un champs meta spécifique-

PlugIn créé:

<?php
/**
 * Plugin Name: VPGredirectplgin
 * Version: 1.0
 * Description: Redirect and update user meta fields after woocommerce purchase | Based on specific product ID. Veuillez modifier directement dans le code du plugin les ID produits, les URL de redirection, le nom du champs à modifier et leur valeur.
 * Author: VPG 
 * Author URI: https://vpgaillard.fr
 */
 
 
 
 
 /**
    * adapted from "LoicTheAztec" at
    * https://wordpress.stackexchange.com/questions/336411/woocommerce-redirect-thankyou-page-based-on-product-id-doesnt-empty-cart
  */
 
 
  /* partie 01 */
  /* fonction de redirection et modification */
 
  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 );
 
    // Get the current user ID (nécessaire pour updater le champs user meta)
    $user_id = $order->get_user_id();
     
  
    // pour le changement de role, on crée la variable user
    $user = new WP_User( $user_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 POUR LES TEST EN DUR
         $product_ids1 = array(11); //artiste
         $product_ids2 = array(14); //orga
          
 
            // HERE define your redirection URL (with the order ID as argument if needed) POUR LES TEST EN DUR
 
    $redirection_url1 = home_url("/?author=" . $user_id . "/");
    $redirection_url2 = home_url("/?author=" . $user_id . "/");
     
 
            // Loop through order items
            foreach( $order->get_items() as $item ) {
                if( in_array( $item->get_product_id(), $product_ids1 ) ) {
         
        //attribution de valeur dans le champs meta   
              update_user_meta( $user_id, 'statut', 'artiste' );
 
           // valide command:
             $order->update_status( 'completed' );
 
           //attribution du rôle:               
             $user->set_role('author');
             
           //redirection vers l'url choisie
                    wp_redirect( $redirection_url1 );
                    exit();
                }
 
                if( in_array( $item->get_product_id(), $product_ids2 ) ) {
 
            //attribution de valeur dans le champs meta   
              update_user_meta( $user_id, 'statut', 'organisateur' );
 
           // valide command:
             $order->update_status( 'completed' );
 
           //attribution du rôle:               
             $user->set_role('author');
             
           //redirection vers l'url choisie
                    wp_redirect( $redirection_url2 );
                    exit();
                }
 
               
            }
        }
    }
}
 
 
/* fin de la fonction de redirection */