skip to Main Content

I need a bit of help with woocommerce customization. I need to redirect the user after the order is completed to a custom page, I’m using this code in my plugin but seems not working

    public function redirect_customer_after_order()
    {
        if( is_checkout() && isset( $wp->query_vars['order-received'] ) ){
            wp_safe_redirect( site_url( 'riepilogo-ordine' ) );
            exit;
        }
    }
    add_action( 'template_redirect', array( $this, 'redirect_customer_after_order' ) );

I’ve tried to place an order but my custom woocommerce elementor page will be not displayed and I will see the default woocommerce thank you page. Also I’ve noticed that this hook will be ignored

remove_action( 'woocommerce_order_details_after_order_table', 'woocommerce_order_again_button' );

It will simple remove the order again button, I’ve placed it inside the __construct() of my plugin. How I can fix these two issues?

2

Answers


  1. Correct Hook: Use woocommerce_thankyou instead of template_redirect for more reliable redirection within WooCommerce

    public function redirect_customer_after_order( $order_id ) {
        wp_safe_redirect( site_url( 'riepilogo-ordine' ) );
        exit;
    }
    add_action( 'woocommerce_thankyou', array( $this, 'redirect_customer_after_order' ) );

    Correct Hook Priority: Use a higher priority to ensure your code runs after WooCommerce’s

    remove_action( 'woocommerce_order_details_after_order_table', 'woocommerce_order_again_button', 10 );
    Login or Signup to reply.
  2. Please Try this code for redirect custom thank you page

    add_action( 'woocommerce_thankyou', 'custom_thankyou_cb');
      function custom_thankyou_cb( $order_id ){
        $order = wc_get_order( $order_id );
        $url = 'https://yoursite.com/custom-custom-thankyou-page';
        if ( ! $order->has_status( 'failed' ) ) {
            wp_safe_redirect( $url );
            exit;
        }
    }

    `

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