Redirect to single product in subcategory
-
Hi,
I have been messing around with the code below (modified version of
- this
) to try to redirect categories with only a single product directly to the product. It’s working after a fashion but both the parent, and child, category links seem to be redirecting to the first product of the first child category.
The structure is Parent Cat > Child Cat > Product. There are no products in the parent categories.
For the life of me I can’t sort out where the problem is, so any help would be hugely appreciated.
Cheers!
add_action( 'template_redirect', '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 = wc_get_product($wp_query->post->ID); if($product){ if ( $product->is_visible() ){ $link = get_permalink($product->post->id); wp_redirect($link, 302 ); exit; } } } return false; }
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Redirect to single product in subcategory’ is closed to new replies.