admin管理员组

文章数量:1287535

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?

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?

Share Improve this question asked Jul 28, 2021 at 12:05 SheldonSheldon 31 bronze badge 1
  • What have you tried so far? – Tony Djukic Commented Aug 5, 2021 at 22:27
Add a comment  | 

1 Answer 1

Reset to default 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 = 'http://example';

      // double check this order has gone through okay and then redirect them
      if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url );
        exit;
      }
    }
}

function woocommerce_redirect_after_checkout(){ global $wp; if (is_checkout()&&!empty($wp->query_vars['order-completed'])){ $redirect_url='https://example'; wp_redirect($rediect_url); exit; } }

本文标签: pluginsHow would I Redirect an existing WooCommerce customer to a specific url after Checkout