Solved! Redirect category if it only has a single product.
-
I wanted to redirect a category to the product page if it only ad a single product.
I searched various hacks, but none worked like I wanted (most did not even worked at all).
After messing a bit I found this sollution:
Add the following code to your fucntions.php:
/* Redirect if there is only one product in the category */ add_action( 'wp_head', 'woocom_redirect_if_single_product_in_category', 10 ); function woocom_redirect_if_single_product_in_category() { global $wp_query; if (is_product_category()) { $cat_obj = $wp_query->get_queried_object(); $catcount = $cat_obj->count; if ($catcount > 1 ){ return; } $product = new WC_Product($wp_query->post->ID); if($product){ if ( $product->is_visible() ){ $link = get_permalink($product->post->id); wp_redirect($link, 302 ); exit; } } } return false; }
This is an adapted sollution from: woocommerce redirect category with single product to product page? (7 posts)
I hope this helps.
- The topic ‘Solved! Redirect category if it only has a single product.’ is closed to new replies.