skip to Main Content

I’m trying to have 2 diferent thank you pages based on 2 categories after payment.

I can redirect to just 1 page if product is on category item and to another one if not.

But i can’t split this for 2 diferent categories, i’m just new on php could be nice if someone could help me with this.

My code:


add_action( ‘template_redirect’, ‘wc_custom_redirect_after_purchase’ );

function wc_custom_redirect_after_purchase() {

global $wp;

if ( is_checkout() && ! empty( $wp->query_vars[‘order-received’] ) ) {

$cat_in_cart = false;
$order_id = isset( $wp->query_vars[‘order-received’] ) ? intval( $wp->query_vars[‘order-received’] ) : 0;
$order = new WC_Order( $order_id );
$product_categories = array( ‘mezcla-y-mastering-online’, ‘asesoria-personalizada’ );

foreach( $order->get_items() as $item ){
if( has_term( $product_categories, ‘product_cat’, $item->get_product_id() ) ) {
$cat_in_cart = true;
break;
}
}

if ( $cat_in_cart) {
if(in_array(«asesoria-personalizada», $product_categories)){
wp_redirect( «https://artchitectsproductions.com/gracias-compra-asesoria/»);
} elseif (in_array(«mezcla-y-mastering-online», $product_categories)){
wp_redirect(«https://artchitectsproductions.com/gracias-compra-mezcla-y-mastering-online/»);
}

} else {
wp_redirect(«https://artchitectsproductions.com/gracias-por-tu-compra/»);
}
exit;

}
}
}

2

Answers


  1. The following code using dedicated template_redirect hook and WordPress has_term() conditional function (to be used with product categories), will redirect customers after checkout to my account section when their order contain items from defined product categories:

    add_action( 'template_redirect', 'order_received_redirection_to_my_account' );
    function order_received_redirection_to_my_account() {
        // Only on "Order received" page
        if( is_wc_endpoint_url('order-received') ) {
            global $wp;
    
            // HERE below define your product categories in the array
            $categories = array('Tshirts', 'Hoodies', 'Glasses');
    
            $order = wc_get_order( absint($wp->query_vars['order-received']) ); // Get the Order Object
            $category_found = false;
    
            // Loop theough order items
            foreach( $order->get_items() as $item ){
                if( has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
                    $category_found = true;
                    break;
                }
            }
    
            if( $category_found ) {
                // My account redirection url
                $my_account_redirect_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
                wp_redirect( $my_account_redirect_url );
                exit(); // Always exit
            }
        }
    }
    

    Code goes in the function.php file of your active child theme (or active theme). Tested and works.

    Login or Signup to reply.
  2. You can receive all product terms using get_the_terms() and then push them to an array. Try the below code.

    add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
    
    function wc_custom_redirect_after_purchase() {
    
        global $wp;
    
        if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
    
            $cat_in_cart        = false;
            $order_id           = isset( $wp->query_vars['order-received'] ) ? intval( $wp->query_vars['order-received'] ) : 0;
            $order              = new WC_Order( $order_id );
            $product_categories = array();
    
            foreach( $order->get_items() as $item ){    
                $product_cat = get_the_terms( $item->get_product_id() , 'product_cat' );
                if( !empty( $product_cat ) ){
                    foreach ( $product_cat as $cat ) {
                        $product_categories[] = $cat->slug;
                    }
                }
            }
    
            if( !empty( $product_categories ) && in_array('asesoria-personalizada', $product_categories ) ){
                wp_redirect( 'https://artchitectsproductions.com/gracias-compra-asesoria/');
            } elseif ( !empty( $product_categories ) && in_array('mezcla-y-mastering-online', $product_categories ) ){
                wp_redirect('https://artchitectsproductions.com/gracias-compra-mezcla-y-mastering-online/');
            }else {
                wp_redirect('https://artchitectsproductions.com/gracias-por-tu-compra/');
            }
    
            exit;
    
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search