How to change the redirect of the "add to cart" button in woocommerce store
Asked Answered
R

3

5

Let me start by saying sorry that this title is somewhat vague. More specifically the title should be:

How to select the behavior of the "add to cart" button in a woocommerce Storefront theme (probably other themes as well) product archives page (aka shop page), such that it adds the item to the cart and then either stays on the shop page, or redirects to the cart.

But that is a long title...

Raney answered 20/2, 2016 at 3:58 Comment(0)
P
6

Redirected To Custom URL :

function my_custom_add_to_cart_redirect( $url ) {
    $url = get_permalink( 1 ); // URL to redirect to (1 is the page ID here)
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

Redirect To Checkout :

function my_custom_add_to_cart_redirect( $url ) {
    $url = WC()->cart->get_checkout_url();
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

Redirect For Certain Categories :

function my_custom_add_to_cart_redirect( $url ) {
    if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
        return $url;
    }
    $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
    // Only redirect products that have the 't-shirts' category
    if ( has_term( 't-shirts', 'product_cat', $product_id ) ) {
        $url = WC()->cart->get_checkout_url();
    }
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
Pothook answered 20/2, 2016 at 8:54 Comment(1)
No, this doesn't seem to work from an archives page (shop), only from a single product page. I did try to hook into woocommerce_add_to_cart_redirect before I posted this question... Here is a link to someone else's description, which does not explain the scenario exactly, but might give a clue as to why the functions above don't work.Raney
D
3

According to the official WooCommerce documentation enter image description here

You will find in tab Products, section 'Display', the checkbox that does what you need: [ ] Redirect to the cart page after... Cheers.

Douville answered 20/2, 2016 at 4:40 Comment(4)
The "Redirect to the cart page after successful addition" checkbox does cause single product pages to be redirected to the cart upon addition, but it does not seem to work for products in an archive (the shop page)... That, I believe, is what the second check button is for: products in archives.Raney
You are right, both checkboxes are very clear and explicit in their function.Douville
I disagree about the clarity of the second description; for someone who does not know what AJAX is, "Enable AJAX" could mean anything... Also, the lack of punctuation makes it unclear whether you are enabling "AJAX" (affecting the add to cart buttons) or you are enabling the "AJAX add to cart buttons". Those are two very different implications, the second might imply that the "add to cart" buttons themselves might not be displayed if the box is not checked; but indeed they are. And if you take it as the first implication, then it still implies nothing about redirection.Raney
for someone who does not know what ANYTHING is, ANYTHING could mean anything... Maybe that's why you should start reading the docs before trying hookers and filtersDouville
R
0

There may be a way to do this with woocommerce hooks and filters, but after spending some time trying to solve this problem that way, I stumbled across the easy solution which was not entirely obvious at first.

The Solution: Check or uncheck the "Enable AJAX add to cart buttons on archives" button under WooCommerce > Products > Display > Add to Cart Behavior section of the WP admin area.

That is apparently exactly what this button is for, however, it seems to me that the title of this checkbox is very misleading and is why it took me so long to find this solution. It should probably be changed to something more like: "Redirect to the cart page after successful addition from archive (shop)". The use of the acronym AJAX is unnecessary and very misleading as to the effect of the checkbox...

Raney answered 20/2, 2016 at 3:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.