skip to Main Content

I used a code to redirect the buyer to a customised page. But I would like to add a different redirect per product category as set in Woocommerce.

This is the code I’m using to redirect for all purchases. Now I need to change it to a redirect per categegory. For example if someone buys a shop product it goed to page A and when they purchase a course it goes to page B after purchase.

<?php

/* Redirect user after check out */
add_action( 'template_redirect', 'jay_custom_redirect_after_purchase' ); 
function jay_custom_redirect_after_purchase() {
    global $wp;
    
    if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
        wp_redirect( 'http://www.yoururl.com/your-page/' );
        exit;
    }
}

2

Answers


  1. In my suggestion I changed the action you selected to allow get order id. So with this informations I obtained the order items and its categories.

    add_action( 'woocommerce_thankyou', 'jay_custom_redirect_after_purchase', 10, 1 ); 
    function jay_custom_redirect_after_purchase($order_id) {
    
    $order = wc_get_order( $order_id );
    $items = $order->get_items();
    $categories_to_A = array('Shop');
    $categories_to_B = array('Course');
    
    foreach ( $items as $item ) {
        if ( has_term( $categories_to_A, 'product_cat', $item['product_id'] ) ) {
            wp_redirect( 'http://www.yoururl.com/your-page-a/' );
            exit;
        }
    
        if ( has_term( $categories_to_A, 'product_cat', $item['product_id'] ) ) {
            wp_redirect( 'http://www.yoururl.com/your-page-b/' );
            exit;
        }
    
        
    }
    
    }
    
    Login or Signup to reply.
  2. You can use the woocommerce_thankyou hook.

    add_action( 'woocommerce_thankyou', 'redirect_after_checkout' );
    function redirect_after_checkout( $order_id ) {
    
        if ( $order->has_status( 'failed' ) ) {
            return;
        }
    
        // set the product category slugs to redirect to
        $url_cat_A = 'https://yoursite.com/category-A';
        $url_cat_B = 'https://yoursite.com/category-B';
    
        $order = wc_get_order( $order_id );
        foreach ( $order->get_items() as $item ) {
            $product = $item->get_product();
            // if the product belongs to category A redirects to the product category page A
            if ( has_term( 'slug_cat_A', 'product_cat', $product->get_id() ) ) {
                wp_safe_redirect( $url_cat_A );
                exit;
            }
            // if the product belongs to category B redirects to the product category page B
            if ( has_term( 'slug_cat_B', 'product_cat', $product->get_id() ) ) {
                wp_safe_redirect( $url_cat_B );
                exit;
            }
        }
    
    }
    

    If the order contains a product from a certain category, it redirects to the corresponding product category page.

    The code must be added to the functions.php of your active theme.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search