Support » Plugin: WooCommerce » Cart & Checkout on same page redirect loop problem

  • Resolved marcolino333

    (@marcolino333)


    Hello Everyone,

    I am using the cart and the checkout shortcode on the same page. Everything works perfect as long as there is at least one item in the cart.

    If I try to delete the last item in the cart, the site calls for /cart/ page but times out with a redirect loop.
    I saw this solution here: https://wordpress.org/support/topic/redirect-loop-occurs-when-calling-for-cart-page-if-cart-is-empty
    but is not working for me.

    I added this to the functions.php file in my theme:

    add_action( ‘init’, ‘woocommerce_empty_cart_redirect’ );
    function woocommerce_empty_cart_redirect() {
    global $woocommerce;
    if ( $_SERVER[“REQUEST_URI”] == “/checkout/” && !sizeof( $woocommerce->cart->get_cart() )) {
    wp_redirect( get_permalink( wc_get_page_id( ‘shop’ ) ) );
    exit;
    }
    }

    Anything I could possibly do? Thanks to everyone!

    https://wordpress.org/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • For anyone else having this problem This solution worked from me. I apsted the following code into functions.php

    function woocommerce_show_checkout() {
    global $woocommerce;

    $cart_number = sprintf(_n(‘%d’, ‘%d’, $woocommerce->cart->cart_contents_count, ‘woothemes’), $woocommerce->cart->cart_contents_count);

    if ($cart_number == 0) {
    return false;
    } else {
    return true;
    }

    }

    add_action(‘template_redirect’, ‘redirection_function’);
    function redirection_function(){
    global $woocommerce;
    $cartNumber = woocommerce_show_checkout();

    if( is_checkout() and $cartNumber == false ) {
    wp_redirect( home_url() ); exit;
    }
    }

    Thanks to Caleb Burks here: https://wordpress.org/support/topic/woocommerce-empty-cart-makes-redirect-loop-error?replies=3

    Hi. So this one solves the redirect loop issue, but breaks the page to not redirect after order has been received.

    How did you solve that?

    Yeah as @dgooglr says it works for the empty cart, but when you place an order it redirects to the home page instead of to the “order-received” page when paying by “Cash On Delivery”.

    Any ideas anyone?

    I just ran into the same problem so I improved the script posted above and posted to a blog article with a few lines of explanation:
    http://etzelstorfer.com/en/woocommerce-redirect-loop-when-cart-and-checkout-on-the-same-page/

    I’ll test this on my site soon! Thanks @haet!

    Any chance you could explain how it works?

    The only thing I added is a check for a $_GET parameter called “key”. This parameter defines a submitted order.

    Thanks a lot! This made a lot of sense.

    So one question, when you now have cart and checkout on the same page. WHen I tried with your code, I got this Shopping cart is empty on top of the “order received page” that the user is shown after a purchase.

    Do you have any good ideas on how to solve that, as I want this message to show in the cart/checkout page but not in the order received side.

    Thanks

    I think you should solve this in your template file. I really depends on your theme.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Cart & Checkout on same page redirect loop problem’ is closed to new replies.