[Resolved] Redirect menu cart icon direct to checkout

Home Forums Support [Resolved] Redirect menu cart icon direct to checkout

Home Forums Support Redirect menu cart icon direct to checkout

  • This topic has 5 replies, 2 voices, and was last updated 1 month ago by Elvin.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2052834
    Ben

    Hi,
    I have ‘Display cart in menu’ activated in Customize > Layount > Woocommerce so the little cart icon appears in my nav menu. When you click on the icon it goes to the Woocommerce Cart page. I am trying to get it so that when you click the cart icon you are redirected to the Woocommerce Checkout page instead. Normal wordpress redirects in the functions.php file in my child theme, or .htaccess redirects, don’t work for some reason – you always go to the cart. Can you help redirect straight to checkout? Thanks.

    #2054013
    Elvin
    Staff
    Customer Support

    Hi there,

    esc_url( wc_get_cart_url() ) is hardcoded as the link of the cart button.

    If you wish to redirect /cart to /checkout, I suggest getting a redirection plugin to achieve this.

    But I advise not doing this as this will be a user experience issue. The point of having a “cart” page is for users to have a final list to edit before paying.

    The site effectively makes it hard for users to edit this list for checkout if the /cart step is skipped.

    A wise man once said:
    "Have you cleared your cache?"

    #2054161
    Ben

    Thanks Elvin, I understand what you are saying, but I have a plugin that allows product quatities to be changed or products deleted on the checkout page so that solves that issue. Is there any way to put something in the functions.php file that changes the hardcoded esc_url( wc_get_cart_url() ) so it goes to checkout instead?

    #2054172
    Elvin
    Staff
    Customer Support

    Is there any way to put something in the functions.php file that changes the hardcoded esc_url( wc_get_cart_url() ) so it goes to checkout instead?

    Having done a second look, technically, you can filter the whole HTML output for the cart to modify the URL.

    But that is going to be a long series of filters. Before going there, you may want to consider just redirecting the cart page to the checkout page with this PHP snippet.

    add_action('template_redirect','redirect_to_checkout');
    function redirect_to_checkout(){
        if ( is_page( 'cart' ) || is_cart() ) {
            wp_safe_redirect(esc_url( wc_get_checkout_url() ));
            exit(); 
        }
    }

    A wise man once said:
    "Have you cleared your cache?"

    #2054195
    Ben

    Thanks Elvin, that works great, but the problem is, if when in checkout someone deletes the only product they have from the checkout page, leaving nothing in the cart (which they can do with the plugin I have), then the checkout tries to send them back to cart, which goes back to checkout, back to cart etc in a big messy loop! It seems then, with that in mind, your original advice is best, to leave the cart icon pointing to the cart. Most people won’t use it anyway as we tend to sell single products, and when people add a product via the product page they go straight to the checkout anyway.

    I very much appreciate your help and time on this,

    Ben

    #2055121
    Elvin
    Staff
    Customer Support

    No problem. 😀

    A wise man once said:
    "Have you cleared your cache?"

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.