Search code examples
phpwordpresswoocommercehook-woocommerce

Change Woocommerce checkout end points to show order summary details


I have a woocommerce website & using PayU payment system. As of now when customer order fails, then the redirection is happening to order-pay endpoint & when order is success, page is redirecting to order-received endpoint. I need customer to redirect to a specific custom url when order fails and for success order, instead of redirecting to order-received endpoint, I would like to show the order summary details & prevent user from redirecting to home page.

I tried the below in functions.php

add_action( 'woocommerce_thankyou', 'test_func');

function test_func( $order_id ) {
    $order = wc_get_order( $order_id );
    $url1 = 'https://yoursite.com/custom-url-1';
    $url2 = 'https://yoursite.com/custom-url-2';

    if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url1 );
        exit;
    } else {
        wp_safe_redirect( $url2 );
        exit;
    }
}

But still it is redirecting to the checkout end points mentioned. see checkout endpoints

I know that it is taking from the woocommerce checkout endpoints mentioned in the Advance Section, but can somebody please help me to find a workaround for this?

Any help would be really appreciated.

Thanks in Advance.


Solution

  • I have prepared a solution for your problem. I have tested your code and it looks like woocommerce has changed something or I don't know and this kind of code is not working anymore. But no worries. I have searched over the internet and found a good solution for you which I have prepared and tested on my staging website and it works great.

    You need to use template_redirect and set it for checkout endpoint url (your case - order-received). Then you need to do the rest with the right functionality to make it work as you expect. Put my code bellow into the functions.php file of your theme and your problem should be solved. Do not forget to change endpoint url with the endpoint url of your website (order-received) and then change the url for failed order status (google.com) and other statuses (yahoo.com)

    add_action( 'template_redirect', 'wc_thank_you_redirect' );
    
    function wc_thank_you_redirect(){ 
      if( isset( $_GET['key'] ) && is_wc_endpoint_url( 'order-received' ) ) { //change order-received to your endpoint if you will change it in the future
        
        $order_id = wc_get_order_id_by_order_key( $_GET['key'] );
     
        if ( ! empty( $order_id ) ) {
          $order = wc_get_order( $order_id );
          $order_status = $order->get_status();
            
            if ('failed' == $order_status ) {  //failed order status
              wp_redirect( 'https://google.com' ); //change url here
              exit;
            } else {
              wp_redirect( 'https://yahoo.com' ); //change url here for other statuses redirect
              exit;
            }
          }
        }
      }
    

    To do this with order-pay you could easily modify my function and change your endpoint url to order-pay and remove else from the function. To display order summary you need to set template for your new URL. Maybe it is better to redirect only failed orders and modify existing woocommerce template for your thank you page.