Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Show next\prev products from current product category (when viewing a single product) 1. If product is last -> Next product is first 2. If product is first -> Prev product is last forked from https://gist.github.com/2176823 (This question was in http://stackoverflow.com/questions/13597687/woocommerce-get-next-previous-product/13612387
<?php
// get next and prev products
// Author: Georgy Bunin (bunin.co.il@gmail.com)
// forked from https://gist.github.com/2176823
function ShowLinkToProduct($post_id, $categories_as_array, $label) {
// get post according post id
$query_args = array( 'post__in' => array($post_id), 'posts_per_page' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $categories_as_array
)));
$r_single = new WP_Query($query_args);
if ($r_single->have_posts()) {
$r_single->the_post();
global $product;
?>
<ul class="product_list_widget">
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php echo $label; ?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
</ul>
<?php
wp_reset_query();
}
}
if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
// get all posts in current categories
$query_args = array('posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
)));
$r = new WP_Query($query_args);
// show next and prev only if we have 3 or more
if ($r->post_count > 2) {
$prev_product_id = -1;
$next_product_id = -1;
$found_product = false;
$i = 0;
$current_product_index = $i;
$current_product_id = get_the_ID();
if ($r->have_posts()) {
while ($r->have_posts()) {
$r->the_post();
$current_id = get_the_ID();
if ($current_id == $current_product_id) {
$found_product = true;
$current_product_index = $i;
}
$is_first = ($current_product_index == $first_product_index);
if ($is_first) {
$prev_product_id = get_the_ID(); // if product is first then 'prev' = last product
} else {
if (!$found_product && $current_id != $current_product_id) {
$prev_product_id = get_the_ID();
}
}
if ($i == 0) { // if product is last then 'next' = first product
$next_product_id = get_the_ID();
}
if ($found_product && $i == $current_product_index + 1) {
$next_product_id = get_the_ID();
}
$i++;
}
if ($prev_product_id != -1) { ShowLinkToProduct($prev_product_id, $cats_array, "next: "); }
if ($next_product_id != -1) { ShowLinkToProduct($next_product_id, $cats_array, "prev: "); }
}
wp_reset_query();
}
}
?>
@Sternberg

This comment has been minimized.

Copy link

Sternberg commented Jun 12, 2013

Is there a way to make this work when the products have been sorted manually? Thanks for any hint!

@human-a

This comment has been minimized.

Copy link

human-a commented Sep 22, 2013

first_product_index is not defined, I get a php warning for that.

@georgybu

This comment has been minimized.

Copy link
Owner Author

georgybu commented Sep 22, 2013

@Sternberg - you can manual order your products with $query_args
@CapitalH - Ou... you totaly right. $first_product_index is never used.

You can check first_product_index like

if ($i == $r->post_count - 1) { $first_product_index = $i; }

See more in WP Query Properties

The idea is to check if

$i == 0                                 => first product
$i == $r->post_count       => last product
@philipb2

This comment has been minimized.

Copy link

philipb2 commented Mar 29, 2014

Great work georgybu.

I got it to work without any problems but I am hoping you can help me add a class to the "next group" as I want that to float right while "prev group" stays to the left.

Appreciate any help you can provide.

Thanks

@lindorblu

This comment has been minimized.

Copy link

lindorblu commented Apr 9, 2015

Hi Georgi!
I'm using your code and it works very well. Thank you!
I wish to know if is possible and how to do, to sort products by tags and by slug or title.
Could you help, please?

Thanks

@apeximum

This comment has been minimized.

Copy link

apeximum commented Jan 10, 2016

how the next and previous buttons will appear in single product page?

@E-VANCE

This comment has been minimized.

Copy link

E-VANCE commented Jan 12, 2016

Superb!! Just what I was looking for, excellent work mate. Works like a charm.

@lexthor

This comment has been minimized.

Copy link

lexthor commented Oct 4, 2016

this works fine as long as a product is in one single category. if prod A is in cat A and cat B the next button sends me to a prod in the last categ, for this example cat B.... can you help me solve this issue? I've searched everywhere and I can't find anything.
thanks!

@jasperf

This comment has been minimized.

Copy link

jasperf commented Dec 17, 2016

@lexthor Did you ever solve this issue yourself with products in multiple categories?

@fromvaldivia

This comment has been minimized.

Copy link

fromvaldivia commented Feb 15, 2017

@georgybu Hello, very good job, I just want to know if you can do that only with the sub categories of a product?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.