WordPress.org

Ready to get started?Download WordPress

Forums

woocommerce redirect category with single product to product page? (7 posts)

  1. bowenac
    Member
    Posted 1 year ago #

    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?

  2. Sam
    Member
    Posted 1 year ago #

    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;
    		}
    	}
    }
  3. jjbbrr
    Member
    Posted 1 year ago #

    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/

  4. grapesky
    Member
    Posted 1 year ago #

    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!

  5. Sam
    Member
    Posted 1 year ago #

    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.

  6. jim-camomile
    Member
    Posted 11 months ago #

    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.

  7. patelprashant8490
    Member
    Posted 5 months ago #

Topic Closed

This topic has been closed to new replies.

About this Topic