• 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;
    }

    https://www.ads-software.com/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Sounds like you need to build in a condition to see if a category has children, and if it does, do no redirect.

    https://codex.www.ads-software.com/Function_Reference/get_term_children

    Thread Starter coldrake

    (@coldrake)

    That makes sense. Thanks!

    If I am understanding correctly I need to feed the get_term_children function the term id and taxonomy name. I’ve been trying to get those values for the current category but everything comes back empty. Clearly there is something I’m not getting.

    I’ve tried get_term_by and get_queried_object and a few others but, no matter whether parent or child category, they are always empty. Are these dependant on a post id to get the related taxonomy info?

    Any hints?

    Thanks!

    Plugin Contributor Mike Jolley

    (@mikejolley)

    get_term_children requires the category ID, and taxonomy ‘product_cat’. What ID are you giving it?

    Thread Starter coldrake

    (@coldrake)

    That’s the problem. Everything I try to pull up the id of the current category comes back empty.

    Plugin Contributor Mike Jolley

    (@mikejolley)

    I’d suggest you var_dump what you’re trying to pass. get_queried_object_id might be the function you want.

    Thread Starter coldrake

    (@coldrake)

    Thanks for the quick response.

    I’ve tried both

    $queried_object = get_queried_object();
     var_dump( $queried_object );
    $catid = get_queried_object_id();
    var_dump ($catid);

    Regardless of whether I’m on a category or product page; the first one returns NULL, and the second returns int(0).

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Where are you placing this code, and are you definitely on a category archive?

    This following code is working for me, but I wanted to redirect anytime there is a single item on an archive page. Tags and Categories:

    /* Redirect if there is only one product in the category or tag, or anywhere... */
    
    function redirect_to_single_post(){
        global $wp_query;
        if( is_archive() && $wp_query->post_count == 1 ){
            the_post();
            $post_url = get_permalink();
    		wp_safe_redirect($post_url , 302 );
    	exit;
        }
    }
    add_action('template_redirect', 'redirect_to_single_post');
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Redirect to single product in subcategory’ is closed to new replies.