Woocommerce: How to automatically redirect to the single product if there is only one product on a category page?
<p>I'm trying to skip the category archive page and go straight to the single product page when there is only one product in the category. </p>

<p>Here is the code I have so far, (not working): </p>

<pre><code>add_action( 'template_redirect', 'woo_redirect_single_product_cat', 10 );

function woo_redirect_single_product_cat() {

global $wp_query, $wp;;

if  ( is_post_type_archive( 'product' ) &amp;&amp; 1 === $wp_query-&gt;found_posts  ) {

    $product = wc_get_product($wp_query-&gt;post-&gt;ID);

       if ( $product &amp;&amp; $product-&gt;is_visible() ) {
            wp_safe_redirect( get_permalink( $product-&gt;id ), 302 );
            exit;

       }
}
}
</code></pre>

<p>This code is modeled after the single search redirect in Woocommerce: </p>

<p><a href="https://github.com/woothemes/woocommerce/blob/fce8dc0868439474c24f7317af50ce7627f0d1c1/includes/wc-template-functions.php#L43" rel="nofollow">https://github.com/woothemes/woocommerce/blob/fce8dc0868439474c24f7317af50ce7627f0d1c1/includes/wc-template-functions.php#L43</a></p>

<p>Thanks!</p>