Created
August 3, 2016 18:35
-
-
Save jamc92/a9a9dafabdd284e870539e3b72702577 to your computer and use it in GitHub Desktop.
WooCommerce conditionally redirect users after add to cart - Category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if we need to use multiple categories?