skip to Main Content

I’m trying to work on something new for displaying 2 different thank you pages for the woocommerce plugin. When a user has selected a different shipping method like local pickups I want to redirect them to different thank you pages after checkout.

I have added the following code but it doesn’t works redirects to the default. What am I doing wrong

// Thank you page redirect
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
    global $wp;
    if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
        wp_redirect( 'https://mywebsite.com/thank-you-page-1/' );
        exit;
    }
    elseif( $order->has_shipping_method('local_pickup:7') ) {
        wp_redirect( 'https://mywebsite.com/thank-you-page-2/' );
        exit;
    }
    elseif( $order->has_shipping_method('flat_rate:1') ) {
        wp_redirect( 'https://mywebsite.com/thank-you-page-3/' );
        exit;
    }
}

Not sure what’s happening, might be I’m missing or doing something wrong.

3

Answers


  1. Does it have to be a redirect? I don’t know exactly what you’re up to, but maybe it is enough to display different content, depending on the shippment method? If so, you can edit the thankyou.php template.

    First copy the file plugins/woocommerce/templates/checkout/thankyou.php to your theme directory themes/(your-theme)/woocommerce/checkout/tankyou.php. Here you can change the thankyou template, without losing the changes after a woocommerce update. (Template structure & Overriding templates via a theme)

    Now you can adjust the thankyou.php (inside your theme directory) and add your conditions at the right place:

    if ( $order->has_shipping_method('local_pickup:7') ) {
        echo "thank you content 2";        
    }
    elseif ( $order->has_shipping_method('flat_rate:1') ) {
        include 'path-to/my-tankyou-content3-file.php';
    }
    
    Login or Signup to reply.
  2. //Use this hook to target your thankyou page.

    <?php
    
    add_action( 'woocommerce_thankyou', 'your_function_name'); 
    
    function your_function_name($order_get_id){
    
    // Here comes your code
    
    }
    
    Login or Signup to reply.
  3. In your function, you don’t define $order. You need to get the $order Object before you can access any of the methods. Since $order is not defined, the other cases elseif are never true.

    // Thank you page redirect
    add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
    function woo_custom_redirect_after_purchase() {
        global $wp;
        // Get the order object
        $order = new WC_Order($wp->query_vars['order-received']);
        if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
            wp_redirect( 'https://mywebsite.com/thank-you-page-1/' );
            exit;
        }
        elseif( $order->has_shipping_method('local_pickup:7') ) {
            wp_redirect( 'https://mywebsite.com/thank-you-page-2/' );
            exit;
        }
        elseif( $order->has_shipping_method('flat_rate:1') ) {
            wp_redirect( 'https://mywebsite.com/thank-you-page-3/' );
            exit;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search