Between sketching on the playground with my toddler (more on that below code), I spend a fair bit of time staring at code. I’d probably spend a lot less time if I wasn’t such a noob, and it took me forever to figure out this Woocommerce problem I’d been having. If you’re trying to redirect your “add to cart” from a specific product category to a specific page, insert this bit of code into your “functions.php”, and you’ll be good to go (it at least worked for me). Take note of where you need to edit the code to suit your pages on your website. I hope this saves you some of that ‘noob’ time.
// redirects to same page
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’] ) );
// Change ‘sandals’ category to your custom category and watch it redirect to your custom page ID below
if ( has_term( ‘sandals’, ‘product_cat’, $product_id) ) {
$url = get_permalink( 1 ); // 1 is the page ID to where you want everything to redirect after clicking “add to cart” button
}
return $url;
}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘my_custom_add_to_cart_redirect’ );
In other news, I often go to the playground with my toddler. Only half the time does she demand my attention to play with her. I use the other half of that time to stare at my phone. Today was a bit different, though. This morning I learned that my email may have been hacked (unconfirmed as of yet). But, I decided to change the passwords. In doing so, it magically logged me out of Facebook most things I use to waste time if I have that kind of time. I have the good fortune of being too lazy to re-log myself into everything with a very long and complicated password, so I decided to trade in my phone for the un-glow of my pocket sketchbook. This image you see was the result. It felt nice to do some “free sketching” again.
Code sources:
- https://stackoverflow.com/questions/35518938/how-to-change-the-redirect-of-the-add-to-cart-button-in-woocommerce-store
- http://jeroensormani.com/redirect-users-after-add-to-cart/
Leave a Reply