4

Now, hitting the "add to chart" button on the archive page will add product to cart, but will also redirect custommer to the page of certain product, and I am trying to disable any redirection after hitting "add to cart" button. I want custommer to stays at the same page where he has been before hitting the button, or just to "refresh" page after adding to cart.

Any suggestions?

/**
 * Redirect subscription add to cart to checkout page
 *
 * @param none
 */
function add_to_cart_checkout_redirect() {
        wp_safe_redirect( get_permalink( get_option(
           'woocommerce_checkout_page_id' ) ) );
        die();
  }
add_action( 'woocommerce_add_to_cart',  'add_to_cart_checkout_redirect', 11
);
momciloo
  • 887
  • 1
  • 11
  • 24

5 Answers5

9

WooCommerce Settings

WooCommerce by default provides the setting for you. Just check if this solution fits your requirement.

Domain
  • 11,562
  • 3
  • 23
  • 44
8

Try to use wp_get_referer as shown

add_filter( 'woocommerce_add_to_cart_redirect', 'wp_get_referer' );
Fungie
  • 113
  • 1
  • 7
3

Here is the answer. If you want custom redirection, there is a filter for that:

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_redirect_function' );
    function custom_redirect_function() {
    return get_permalink( wc_get_page_id( 'shop' ) );
}
momciloo
  • 887
  • 1
  • 11
  • 24
1

Please add the following code to the file functions.php

/**
 * Set a custom add to cart URL to redirect to
 * @return string
 */
function custom_add_to_cart_redirect() { 
    return 'http://www.yourdomain.com/your-page/'; 
}
add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );

Thanks

Rahul S
  • 643
  • 5
  • 11
  • Thank you. How can I replace "http://www.yourdomain.com/your-page/" with a page where custommer was when he clicked at the "add to cart" button? Thanks – momciloo Jun 23 '15 at 09:28
  • You can add the page where you perform the add to cart itself. So it will redirect to same page. – Rahul S Jun 23 '15 at 09:30
  • No, problem is that I need to stay at the same archive or product page, where I pushed "add to chart" button – momciloo Jun 23 '15 at 09:34
  • Then i think you can give the product page url like $url = get_permalink( $product_id ); and get the current product page url and give there. – Rahul S Jun 23 '15 at 09:37
  • This is what's happening now. When I add to cart it redirects me to the product page, but I need to stop any redirection. – momciloo Jun 23 '15 at 09:48
  • Did you get the current product page with product name or product id. Then try to give that there. – Rahul S Jun 23 '15 at 09:50
0

Use remove action

remove_action( 'woocommerce_add_to_cart',  'add_to_cart_checkout_redirect', 1);
sarath
  • 698
  • 6
  • 20
  • Does not work, still redirecting me to the page of certain product – momciloo Jun 23 '15 at 09:31
  • make sure add_action( 'woocommerce_add_to_cart', 'add_to_cart_checkout_redirect', 11 ); is causing the redirection . comment it and check . I think it should redirection to the checkout page not the product page – sarath Jun 23 '15 at 09:37