Home › Forums › WoodMart support forum › Product Prev/Next Navigation Bug
Product Prev/Next Navigation Bug
- This topic has 5 replies, 2 voices, and was last updated 6 years, 6 months ago by Artem Temos.
-
AuthorPosts
-
June 11, 2018 at 5:12 pm #62317
rmbaumerParticipantBug: Prev/Next arrows seem to take you to a random product instead of the previous or next product in the current category
The file
content-single-product.php
generates this code using the functionwoodmart_products_nav()
which exists in file/inc/woocommerce.php
Uses wp functions
get_next_post()
andget_previous_post()
– I added the necessary args to restrict to prev/next in same category:true, '', 'product_cat'
but it is still not a great solution because the underlying wp functions base prev and next on post published time. If two posts are published at the same time then the other posts with the same time are skipped altogether (this is often the case with products that have been imported, the post time will be within the same second).Also, this is a bit confusing anyway because if a user, for example, is expecting the prev/next to take them to the same prev/next as it was listed in order on the product category page – In this case alphabetically – it will be diffnt. Though this can’t really work like that because the product pages can be re-sorted by the user on the frontend, so how with the prev/next on the product page know what the user was expecting? Example: prev/next by newness, price, alphabetical order, etc…
June 11, 2018 at 5:56 pm #62325
Artem TemosKeymasterHello,
We are glad to know that you have considered using WoodMart for your web-site. I hope you will be happy with it.
By default, the WooCommerce plugin doesn’t have products next/previous arrows functionality at all. To implement this feature we use standard WordPress PHP
get_next_post
andget_prev_post
functions that can obtain adjacent posts from the database. Unfortunately, it takes products from the whole table and depends only on the shop page order. The system can’t know that you previously visited this product’s category page. Yes, it would be much better to show next/prev product from the same category. But how the system might know which one? The product can be from multiple categories. Then, from which category next product should be taken? Or for example, if I use filter by price or by color/size the order for products changes again. And naturally, that when you open some product from that page you must see products from the same color/size or from the same price filter interval. And there are a lot of such cases that make it impossible to make product arrows buttons work as expected.We spent a lot of time investigating this issue but, unfortunately, there is no solution for this problem. You can check any other theme for WooCommerce and you will see the same issue.
Kind Regards
XtemosJune 12, 2018 at 2:34 pm #62495
rmbaumerParticipantToo bad, that seemed like a really cool feature, but as it currently works, I feel that it is more confusing to the end user than it is helpful for product navigation.
I do understand that a solution would be quite complex. Thanks for providing additional insight into potential pitfalls (product in multiple categories, etc). If I have time, I’ll try to find a way to get this to work how I’d like and will post back here if I come up with a solution that can be applied universally.
Maybe something like how it’s done here: https://gist.github.com/georgybu/4285005 instead of using
get_next_post()
andget_previous_post()
Anyway, thanks for quick response and great job with the Woodmart theme – overall I love it.
June 12, 2018 at 5:41 pm #62524
Artem TemosKeymasterWe will investigate this solution as well.
Thank you!
June 12, 2018 at 8:02 pm #62540
rmbaumerParticipantThanks! In case this is useful to anyone else out there, I got this working to suit my needs for now by using the code below. As mentioned above, if a product is in more than one category, there will be issues. For me though, my products are mostly only in one category each.
Based on this: https://wordpress.stackexchange.com/a/204270/145210
add_filter('get_next_post_sort', 'filter_next_and_prev_post_sort'); add_filter('get_previous_post_sort', 'filter_next_and_prev_post_sort'); function filter_next_and_prev_post_sort($sort) { $op = ('get_previous_post_sort' == current_filter()) ? 'DESC' : 'ASC'; $sort = "ORDER BY p.post_title ".$op ." LIMIT 1"; return $sort; } add_filter( 'get_next_post_join', 'navigate_in_same_taxonomy_join', 20); add_filter( 'get_previous_post_join', 'navigate_in_same_taxonomy_join', 20 ); function navigate_in_same_taxonomy_join() { global $wpdb; return " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; } add_filter( 'get_next_post_where' , 'filter_next_and_prev_post_where' ); add_filter( 'get_previous_post_where' , 'filter_next_and_prev_post_where' ); function filter_next_and_prev_post_where( $original ) { global $wpdb, $post; $where = ''; $taxonomy = 'product_cat'; $op = ('get_previous_post_where' == current_filter()) ? '<' : '>'; if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) { return $original ; } $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); $term_array = array_map( 'intval', $term_array ); if ( ! $term_array || is_wp_error( $term_array ) ) { return $original; } $where = " AND tt.term_id IN (" . implode( ',', $term_array ) . ")"; return $wpdb->prepare( "WHERE p.post_title $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_title, $post->post_type ); }
June 13, 2018 at 6:18 am #62558
Artem TemosKeymasterGreat, thank you!
-
AuthorPosts
- You must be logged in to create new topics. Login / Register