The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Solved! Redirect category if it only has a single product. (1 post)

  1. António Pinto
    Posted 2 years ago #

    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 ){
            $product = new WC_Product($wp_query->post->ID);
                if ( $product->is_visible() ){
                    $link = get_permalink($product->post->id);
                    wp_redirect($link, 302 );
        return false;

    This is an adapted sollution from: woocommerce redirect category with single product to product page? (7 posts)

    I hope this helps.


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by António Pinto
  • This topic is not resolved
  • WordPress version: 3.8.1


No tags yet.