0

I am trying to find a solution for redirecting existing customers to a specific url after checkout. I'm not sure if the Thank you page redirect is sufficient for the process. can you assist?

1
  • What have you tried so far? Aug 5 at 22:27
0

The following should suffice. It will redirect to your custom URL only if the order has not failed.

add_action( 'woocommerce_thankyou', 'ii_redirect_thankyou');
function ii_redirect_thankyou( $order_id ){
    $customer_orders = get_posts( array(
        'numberposts' => 2, // getting 2 as by the time this purchase has happened the count will be 1.
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed', // Only orders with "completed" status
        'fields'      => 'ids', // Return Ids "completed"
    ) );

    if ( count($customer_orders) > 1 ) {
      $order = wc_get_order( $order_id );
      $url = get_bloginfo('url') . '/custom-url';

      // double check this order has gone through okay and then redirect them
      if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url );
        exit;
      }
    }
}
3
  • So can this statement identify is they are an existing customer? cause i only want it to work with customers whom have already purchased from me, Not new customers. Im making it now were all have to create accounts no guest purchases.
    – Sheldon
    Jul 28 at 12:23
  • I've adjusted the code to do as you need though I've not actually tested it so tweaks may be required.
    – Steve
    Jul 28 at 12:51
  • I added the code and tried to tweak it a bit, but its not going to the external link i want: this is what i did:
    – Sheldon
    Jul 29 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.