Jump to content

PHP Woocommerce redirection after purchase and validation


Recommended Posts

This code checks firstly if user enters order-received url so that they won't be redirected to upsell-1 page all the time. But the problem is that after purchase i'm being redirected to order received page instead of upsell-1 page.

Maybe validation must be written in different way (if (is_wc_endpoint_url('order-received')))

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    if (is_wc_endpoint_url('order-received')) {
    return;
    }
    WC()->session->set('original_order_id', $order_id);
    $upsell_page_url = home_url('/upsell-1');
    wp_redirect($upsell_page_url);
    exit;
}

 

You can try checking if the current request is for the 'order-received' endpoint using the is_wc_endpoint_url function with the specific order ID. Here's an updated version of your code:

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Check if the current request is for the 'order-received' endpoint with the specific order ID
    if (is_wc_endpoint_url('order-received') && get_query_var('order-received') == $order_id) {
        return;
    }

    WC()->session->set('original_order_id', $order_id);
    $upsell_page_url = home_url('/upsell-1');
    wp_redirect($upsell_page_url);
    exit;
}

 

Alright redirection to upsell works now after purchase after editing this part

from

if (is_wc_endpoint_url('order-received'))

to

if (!is_wc_endpoint_url('order-received'))



but when in upsell-1 page after submitting button new item is added but I keep getting redirected back to upsell-1 page I should be redirected to order-received, I echoed out the url it is correct, but redirection is wrong.

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    if (!is_wc_endpoint_url('order-received')) {
    return;
    }
    WC()->session->set('original_order_id', $order_id);
    $upsell_page_url = home_url('/upsell-1');
    wp_redirect($upsell_page_url);
    exit;
}

add_action('template_redirect', 'process_upsell');
function process_upsell() {
    if (is_page('upsell-1') && isset($_GET['woocommerce_checkout_place_order'])) {
        $original_order_id = WC()->session->get('original_order_id');
        if ($original_order_id) {
            $original_order = wc_get_order($original_order_id);
            $original_order->add_product(wc_get_product('875'));
            $original_order->calculate_totals();
            $original_order->save();
            
            
            $order_received_url = $original_order->get_checkout_order_received_url();
            wp_redirect($order_received_url);
            exit;
        } 
    }
}

 

  On 12/28/2023 at 2:42 PM, Olumide said:

You can try checking if the current request is for the 'order-received' endpoint using the is_wc_endpoint_url function with the specific order ID. Here's an updated version of your code:

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Check if the current request is for the 'order-received' endpoint with the specific order ID
    if (is_wc_endpoint_url('order-received') && get_query_var('order-received') == $order_id) {
        return;
    }

    WC()->session->set('original_order_id', $order_id);
    $upsell_page_url = home_url('/upsell-1');
    wp_redirect($upsell_page_url);
    exit;
}

 

Expand  

With your changes, after purchase I'm being redirected to order-received page straight away, but I need to be redirected to upsell-1 page

Try it this way

 

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Check if not on the 'order-received' page
    if (!is_wc_endpoint_url('order-received')) {
        WC()->session->set('original_order_id', $order_id);
        $upsell_page_url = home_url('/upsell-1');
        wp_redirect($upsell_page_url);
        exit;
    }
}

 

  On 12/28/2023 at 3:06 PM, Olumide said:

Try it this way

 

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Check if not on the 'order-received' page
    if (!is_wc_endpoint_url('order-received')) {
        WC()->session->set('original_order_id', $order_id);
        $upsell_page_url = home_url('/upsell-1');
        wp_redirect($upsell_page_url);
        exit;
    }
}

 

Expand  

Did not work - I'm redirected straight to order-received page

I adjusted it this way

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Check if not on the 'order-received' page
    if (!is_order_received_page()) {
        WC()->session->set('original_order_id', $order_id);
        $upsell_page_url = home_url('/upsell-1');
        wp_redirect($upsell_page_url);
        exit;
    }
}

 

  On 12/28/2023 at 3:13 PM, Olumide said:

I adjusted it this way

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Check if not on the 'order-received' page
    if (!is_order_received_page()) {
        WC()->session->set('original_order_id', $order_id);
        $upsell_page_url = home_url('/upsell-1');
        wp_redirect($upsell_page_url);
        exit;
    }
}

 

Expand  

Instead of relying on the is_wc_endpoint_url function, you can use the is_order_received_page function to check if you are on the order received page.

  On 12/28/2023 at 3:13 PM, Olumide said:

I adjusted it this way

add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Check if not on the 'order-received' page
    if (!is_order_received_page()) {
        WC()->session->set('original_order_id', $order_id);
        $upsell_page_url = home_url('/upsell-1');
        wp_redirect($upsell_page_url);
        exit;
    }
}

 

Expand  

I think with this code i'm still gonna be redirected to order-received page nevertheless because this is just an if check. By the way are you using GPT? I tried it but to no avail.

Edited by userphp10

I use a different approach by using the `wp_get_referer()` function to check the referring page. If the referring page is not the 'order-received' page, then it will proceed with the redirection.

```php
add_action('woocommerce_thankyou', 'upsell_redirect');
function upsell_redirect($order_id) {
    // Get the referring page
    $referer = wp_get_referer();

    // Check if the referring page is not the 'order-received' page
    if ($referer && false === strpos($referer, 'order-received')) {
        WC()->session->set('original_order_id', $order_id);
        $upsell_page_url = home_url('/upsell-1');
        wp_redirect($upsell_page_url);
        exit;
    }
}
```

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.