[BUG-FIX] Redirect Hangs Indefinitely After Adding To Cart

[ad_1]

TLDR; The following changes need to be made to “\wp-event-solution\core\woocommerce\hooks.php”:

public function etn_redirect_checkout_add_cart() {
        $add_to_cart_redirect       = empty( \Etn\Utils\Helper::get_option( 'add_to_cart_redirect' ) ) ? 'event' : \Etn\Utils\Helper::get_option( 'add_to_cart_redirect' );
        if( 'cart' == $add_to_cart_redirect ){
            //return wc_get_cart_url(); // Hangs forever
            wp_redirect( wc_get_cart_url() );
            exit();
        } elseif('checkout' == $add_to_cart_redirect){
            //return wc_get_checkout_url(); // Hangs forever
            wp_redirect( wc_get_checkout_url() );
            exit();            
        }
    }

<hr>

I was stumped with this issue for HOURS before I finally tracked down the cause. Hopefully I can save someone else the trouble:

Replicating the issue:
On my event page, clicking “Buy Ticket” opens the attendee registration prompted. Then, once the attendee info is typed in, clicking submit adds the ticket to the cart.
Unfortunately, the post request was stuck in the processing phase indefinitely.

Locating the cause:
After an unholy number of page refreshes using die() to advance the code one step at a time, I tracked the issue down to the function etn_redirect_checkout_add_cart() (in “/wp-event-solution/core/woocommerce/hooks.php”, around line 300).

From what I can tell this line of code is the issue:
return wc_get_checkout_url()

Fixing the issue:
By replacing the return statement (return wc_get_checkout_url()) with a call to the WP redirect function (wp_redirect( wc_get_cart_url() ); exit();), it fixed the hanging issue, and the page now redirects as intended:

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer