Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamc92/a9a9dafabdd284e870539e3b72702577 to your computer and use it in GitHub Desktop.
Save jamc92/a9a9dafabdd284e870539e3b72702577 to your computer and use it in GitHub Desktop.
WooCommerce conditionally redirect users after add to cart - Category
<?php
/**
* Redirect users after add to cart.
*/
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' );
@napster3p
Copy link

What if we need to use multiple categories?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment