Home Forums WoodMart support forum Product Prev/Next Navigation Bug

Product Prev/Next Navigation Bug

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #62317

    rmbaumer
    Participant

    Bug: 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 function woodmart_products_nav() which exists in file /inc/woocommerce.php

    Uses wp functions get_next_post() and get_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…

    #62325

    Artem Temos
    Keymaster

    Hello,

    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 and get_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
    Xtemos

    #62495

    rmbaumer
    Participant

    Too 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() and get_previous_post()

    Anyway, thanks for quick response and great job with the Woodmart theme – overall I love it.

    #62524

    Artem Temos
    Keymaster

    We will investigate this solution as well.

    Thank you!

    #62540

    rmbaumer
    Participant

    Thanks! 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 );
    }
    #62558

    Artem Temos
    Keymaster

    Great, thank you!

Viewing 6 posts - 1 through 6 (of 6 total)