• dadra

    (@dadra)


    I have my shop page set up to automatically display thumbnails linking to category pages. However, some of my categories only have one product in them, and it’d be great if those links went directly to the single product page rather than to a category page that then shows just one product.

    woocommerce support told me I’d need to tweak content-product_cat.php (though how exactly to tweak it exceeds what they offer for support).

    So I guess I’m looking for a conditional statement like “If only one product in category, create link to the single product page. If more than one product in category, create link to category page.”

    Any ideas? Thanks in advance!

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

Viewing 15 replies - 1 through 15 (of 16 total)
  • I’m very interested in this too. Will post if I figure it out.

    This seems to work:

    In content-product_cat.php

    Swap out

    get_term_link( $category->slug, 'product_cat' )

    with

    ` <?php if($category->count == 1){

    $query_args = array( ‘showposts’=>1,’post_status’ => ‘publish’, ‘post_type’ => ‘product’, ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => $category->slug
    )));

    $r = new WP_Query($query_args);

    if ($r->have_posts()) {
    ?>
    <?php while ($r->have_posts()) : $r->the_post(); global $product;
    $category_link = get_permalink();
    endwhile; ?>
    <?php
    // Reset the global $the_post as this query will have stomped on it
    wp_reset_query();

    } else {$category_link = $category_link = get_term_link( $category->slug, ‘product_cat’ );}

    }else{$category_link = get_term_link( $category->slug, ‘product_cat’ );}?>`

    <a href="<?php echo $category_link; ?>">

    Remember to do this kind of changes via template file overrides in your theme, so your changes do not get lost when you update the WooCommerce plugin. Read our information on Template Structure for more information on where all the files are located: https://www.woothemes.com/woocommerce-docs/codex/template-structure/

    Yes, definitely don’t make those changes to the original template files. Sorry forgot to mention that.

    Copy them from:
    /plugins/woocommerce/templates/

    to:

    /themes/YOURTHEME/woocommerce

    Thread Starter dadra

    (@dadra)

    salonmonster…Way to go! It looks like that pretty much does the trick. The only issue is that it’s messing up the grid layout on my shop page.

    I have 7 categories total, with 4 thumbnails per row, the markup of which looked like this before applying the change:

    <li class="product first">
    <li class="product ">
    <li class="product ">
    <li class="product last">
    <li class="product first">
    <li class="product ">
    <li class="product ">

    Here’s the markup after applying your change:

    <li class="product first">
    <li class="product ">
    <li class="product ">
    <li class="product last">
    <li class="product first">
    <li class="product ">
    <li class="product first">

    From what I can tell it’s causing the class of “first” to be applied to the li immediately following ones containing only a single product. Any thoughts?

    And just for clarity, I replaced this:

    <a href="<?php echo get_term_link( $category->slug, 'product_cat' ); ?>">

    With this:

    <?php if($category->count == 1){
    
    $query_args = array( 'showposts'=>1,'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
    array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => $category->slug
    )));
    
    $r = new WP_Query($query_args);
    
    if ($r->have_posts()) {
    ?>
    <?php while ($r->have_posts()) : $r->the_post(); global $product;
    $category_link = get_permalink();
    endwhile; ?>
    <?php
    // Reset the global $the_post as this query will have stomped on it
    wp_reset_query();
    
    } else {$category_link = $category_link = get_term_link( $category->slug, 'product_cat' );}
    
    }else{$category_link = get_term_link( $category->slug, 'product_cat' );}?>
    
    <a href="<?php echo $category_link; ?>">

    Thanks a ton for taking the time with this!

    Glad I found this post. I needed the same option to link directly to a product if only one in that category existed.

    Implementing the snipped above does just that…however it breaks my grid either adding “first” or “lastcolumn” to every product.

    I’ve been trying for hours, with no luck.

    Any help is greatly appreciated!

    copy templates/content-product.php to /themes/YOURTHEME/woocommerce

    modify lines 31 to 36 to read:

    <li class="product<?php
    if ( ( $woocommerce_loop['loop'] - 1 ) % $woocommerce_loop['columns'] == 0 || $woocommerce_loop['columns'] == 1)
       echo ' first';
    if ( $woocommerce_loop['loop'] % $woocommerce_loop['columns'] == 0 )
         echo ' last';

    ?>”>

    This issue is resolved in v2.0 beta.
    You can also apply the same code to content-product_cat.php for categories.

    Excellent… am looking to do something similar, but would like to link the product thumbnails to a single product…

    Here is a screen shot…

    Yes, i had to use the plugin since 3.5 does not allow the links for images anymore ??

    Thanks swicks.

    I don’t see a reference to ‘<li class=”product……’ in my cotent_product-cat.php

    This is the theme file – maybe it’s overwriting the woocommerce file?

    pastbin file

    What it’s doing is giving me 3 columns, when my theme options is set to 4 – and two columns when set to three. Basically, losing a column on whatever theme setting I use.

    If you can have a look, again, i’d appreciate it! Thank you!

    the info I sent relates to woocommerce’s internal themes.

    Worth checking the source that the div at the start of the category has ‘grid4column’ css class.

    Otherwise speak to your template developer – css problem or page layout effecting your content.

    Thanks to your help I got things working – cheers swicks!

    Thread Starter dadra

    (@dadra)

    @grapesky … Can you list the steps that worked for you? I still can’t get my grid to layout properly when applying salonmaster’s tweak. Thanks!

    Thread Starter dadra

    (@dadra)

    Salonmonster’s code works as advertised but breaks my grid. I’ve tried applying Swicks’s fix for that to both content-product.php and content-product_cat.php, but it doesn’t seem to do anything.

    Hoping for some more detailed guidance if anybody has suggestions. Otherwise I guess I’ll wait for woocommerce v2 and hope they’ve added some settings to address this. Thanks in advance!

    Hi dadra,

    Sorry for the late reply. I don’t not get emails from post replies for some reason?

    Anyway, I got the grid working and a category with a single product will go to the single product page BUT if I have multiple one product cats in the same general category then the cat with the lowest order (or oldest) is shown when any category is clicked.

    For examples I have Category A with sub cats B, C, and D each containing one product. For this example B has the lowest order and was created first. Now clicking on C, or D will lead you to the single product for cat B.

    I tried updated the v2 beta as swicks said above it has been fixed, but no luck that route either!

    Thread Starter dadra

    (@dadra)

    Thanks grapesky. Could you please share the specific steps for how you implemented this? I’m not using sub-categories on the site in question so it sounds like things should work. If you’re able to share the details here that would be great.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Shop page: if only one product in category, link to single product’ is closed to new replies.