Support » Plugins » woocommerce redirect category with single product to product page?

  • Anyone know how to make it so when you click on a category that has only one item go to that items product page instead of going to that category page?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Sam

    (@samholford)

    This is my solution. Hope it helps. (Add to your functions.php file)

    /* Redirect if there is only one product in the category */
    add_action( 'woocommerce_before_shop_loop', 'custom_woocommerce_redirect_if_single_product_in_category', 10 );
    function custom_woocommerce_redirect_if_single_product_in_category ($wp_query) {
    	global $wp_query;
    	if (is_product_category()) {
    		if ($wp_query->post_count==1) {
    			$product = new WC_Product($wp_query->post->ID);
    			if ($product->is_visible()) wp_safe_redirect( get_permalink($product->id), 302 );
    			exit;
    		}
    	}
    }

    I was able to do this by installing the ‘Quick Page/Post Redirect Plugin’ which enabled me to redirect a category to a product page. Plugin details are here: http://wordpress.org/extend/plugins/quick-pagepost-redirect-plugin/

    Hey Sam,

    Can’t seem to get that to work. Getting error with header.php and pluggable.php

    Is there another place I could put the code?

    Thanks!

    Sam

    (@samholford)

    Sorry I wouldn’t really know where to start regarding debugging. It works for me with WP 1.5.1 and the latest WooCommerce. Make sure the code is your theme’s functions.php file.

    Here is one that I made based on Sam’s code. It works except I am having a bit of trouble with “is_visible()”.

    /* 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 (can_show($product->post->id)) {
    if ( $product->is_visible() ){
    //print_r(‘<pre style=”background: #fff;”>’); print_r($product); print_r(”); exit;
    $link = get_permalink($product->post->id);
    //echo “<meta http-equiv=’refresh’ content=’0;url=$link’ />”;
    wp_redirect($link, 302 );
    exit;
    } // if can show
    } // if $product exists
    } // if is category
    return false;
    //}
    }

    notes:

    1. I had trouble also with the redirect throwing the headers already sent error. I found two solutions.

    a. use the alternate <meta> redirect method (code is commented above). It works but I dont know if that has bad implications for SEO in this case.

    b. Changing the hook from woo_head to wp_head made it redirect properly.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘woocommerce redirect category with single product to product page?’ is closed to new replies.