WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Redirect to Checkout only for certain items (10 posts)

  1. Geet Jacobs
    Member
    Posted 1 year ago #

    After many many hours i am stumped on how to achieve this, our site sells products and also memberships and donations. We want products to do the default action of staying on the product page but if its a donation or membership we want it to go directly to checkout.

    I have been working with the add_to_cart_redirect filter, inside the function I have used is_page('donation');, has terms product_cat, global $post, also tried to dig through the global $woocommerce to get some sort of variable to change the redirect to cart for only certain products.

    add_filter('add_to_cart_redirect', 'custom_add_to_cart_redirect');
    
    function custom_add_to_cart_redirect() {
         return get_permalink(get_option('woocommerce_checkout_page_id')); // Replace with the url of your choosing
    }

    Anyone run into this before and have some pointers?

    function redirect_to_checkout() {
          global $woocommerce,$post;
    
    if( has_term( 'membership-donations', 'product_cat', $post->ID ) ){
    
        		$checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
        		 return $checkout_url;
        	};
    }

    Thanks!

    http://wordpress.org/extend/plugins/woocommerce/

  2. jasonkadlec
    Member
    Posted 9 months ago #

    Did you solve it? I'm just looking to do this now, also for donations!

  3. Geet Jacobs
    Member
    Posted 9 months ago #

    Hello Jason,

    I sure did, here you go!

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
            global $woocommerce;
    		//Get product ID
    		$product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['product_id']);
    		//Check if product ID is in a certain taxonomy
    		if( has_term( 'membership-donations', 'product_cat', $product_id ) ){
    					//Get cart URL
    		    		$checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
    		    		//Return the new URL
    		    		return $checkout_url;
    		 };
    }
  4. ryan.odwyer
    Member
    Posted 8 months ago #

    Hi Geet,

    I also am looking at this for donations, but it doesn't appear to be working for me.

    I have a category called "philanthropy" and I have added this code to my functions.php, but it doesn't seem to fire(I did change your code to match my category, even tried it using ID)

    Did I require some extra code somewhere else?

    Thanks,
    Ryan

  5. ryan.odwyer
    Member
    Posted 8 months ago #

    Actually, after some fiddling I changed the $product_id line and this worked for me, don't know why though, I copied from the base woocommerce functions.

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
            global $woocommerce;
    
    		//Get product ID
    		$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
    		//Check if product ID is in a certain taxonomy
    		if( has_term( 'Philanthropy', 'product_cat', $product_id ) ){
    					//Get cart URL
    		    		$checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
    		    		//Return the new URL
    		    		return $checkout_url;
    		 };
    }
  6. BlottedInq
    Member
    Posted 3 months ago #

    Not to completely revive a dead topic, but I am looking to do what is listed above, but instead redirect to the CART opposed to the checkout. I tried the suggestion by ryan.odwyer and it worked, but I would really like to have a redirect to CART for a category instead of CheckOut.

  7. Geet Jacobs
    Member
    Posted 3 months ago #

    Hello Blottedinq,

    You only want to redirect to the cart for certain products?

    I think you could use this below:

    $checkout_url = $woocommerce->cart->get_cart_url();

    Replacing this:

    $checkout_url = get_permalink(get_option('woocommerce_checkout_page_id'));
  8. BlottedInq
    Member
    Posted 3 months ago #

    Worked perfectly!!! Thank you so much Geet Jacobs!

  9. BlottedInq
    Member
    Posted 2 months ago #

    Another question for anyone still reading :-)

    I am wondering if I can add more than one filter? As in, redirect some items to the cart, and others to different pages of my site. I tried to just use the working lines given above again, but it gave me an error.

    Fatal error: Cannot redeclare redirect_to_checkout() (previously declared in /home/content/60/11977160/html/wp-content/themes/virtue_premium/functions.php:116) in /home/content/60/11977160/html/wp-content/themes/virtue_premium/functions.php on line 143

    Is this possible?

  10. BlottedInq
    Member
    Posted 2 months ago #

    And just so I am clear, i replaced the

    $woocommerce->cart->get_cart_url()

    with

    'www.urlofmychoice.com'

    and it allowed me to redirect specific items in a category to a different part of my site. I was just running into issues when I tried to add another filter to affect a separate category

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.