Reputation: 11
I'm building a site where you get a free item (you can choose what color) with your purchase of another item category. Is there a way to make the 1 category type, when you click on add to cart, gets redirected to the one free product? All other categories however would not be redirected.
Upvotes: 1
Views: 646
Reputation: 43
You can add a redirect depending on what product ID was added to the cart (Through the "added-to-cart" GET variable). And then getting the category ID from that.
// Add redirect action
add_action( 'template_redirect', 'check_cart_product', 0 );
function check_cart_product(){
// get product ID, then category ID
$product_ID = $_GET['added-to-cart'];
$term_list = wp_get_post_terms($product_ID,'product_cat',array('fields'=>'ids'));
$cat_id = (int)$term_list[0];
// check if product is from specific category, then redirect
if($cat_id == 25)
wp_redirect(get_permalink(352));
}
Upvotes: 1