Marketpress: Previous / Next Product button from product view page

I am looking for a easy way to add a link to the "Previous product" and "Next product" on my mp_product.php page, is there a shortcut code built in for this already?

  • Patrick

    Hi there @Monte

    I hope you're having a great day!

    Yes indeed, there is!

    You can either use the template-tag like so:
    <?php mp_store_navigation () ; ?>

    Or use the navigation parameters in a shortcode like so:
    <?php echo do_shortcode ('[mp_list_products paginate="true" page="1" per_page="10"]') ; ?>

    ...or for global products in a multisite:
    <?php echo do_shortcode ('[mp_list_global_products paginate="true" page="1" per_page="10"]') ; ?>

    Both the template-tag and shortcodes can accept those pagination parameters. See under Settings > MarketPress in your network admin, and Products > Store Settings > Shortcodes in your site admin for all the details about the parameters you can use, and how to use 'em.

    I hope this helps! And thanks for being a member :slight_smile:

  • Monte

    I am looking for a simple "Previous Product" or "Next Product" when viewing a single product

    example: I visit "shirts" category and select the first item, on the item view page I would like to be able to click "View next product" and see the second item without going back to the previous page. While I am viewing the second item, there should be links to "View Previous Product" and "View Next Product", based on the category that I have opened (in this case, "shirts")

  • Michelle Shull

    Hi, Monte!

    Our second level team is very busy, and surprisingly small, so it can take a while longer to hear back from them. They prioritize critical issues over code customizations, but one of our second level team will be through shortly to see if they can help out here. If it's urgent, can I point you to our jobs board? It may be simpler and faster to get something to your exact specifications.

    Thanks for your understanding, Monte!

  • Monte

    For your developers
    What I am needing is a query to the database, within the mp_product_description is fine, or as its own shortcode using the product id

    select {previous|next} id from {store.products} where category = {current category} limit 1,

    then using that id, get the link to the product
    order by id is fine, we just need a consistent prev/next button, and it needs to be consistent with the category

  • Jose

    Hi there Monte,

    Hope you are doing great and thanks for your patience here.

    I would still suggest to use the WP built-in functions for this, as suggested by Predrag above.

    You can make it work as you need by adding setting a few parameters:

    previous_post_link( '%link', 'Prev product in category', TRUE, ' ', 'product_category' );
    next_post_link( '%link', 'Next product in category', TRUE, ' ', 'product_category' );

    In this way, the links will show only the next post sharing at least one taxonomy term.
    The navigation should be consistent and ordered by creation date.

    Please let me know if this works fine for you.

    Cheers,
    Jose

  • Monte

    http://www.pyrocityfireworks.com/clarksville-in/store/products/36-bc-morning-glory/?prev_next=true

    setting a quick session for testing, so customers do not see it...
    this category has 18 products, but when using the code mentioned above it only cycles through 3 products, but those 3 products change every click of the link

    if on product 1: prev is 18, next is 12
    click next to 12: prev is 11, no next
    click prev to 11: prev is 13, next is 9
    click prev to 13: no prev, next is 6

  • Jose

    Hey Monte,

    I see what you mean. That's really odd.

    I would like to take a look into your site, because I'm afraid that if the WP method is failing the custom query will likely behave the same.

    Please send me the following information via contact form :

    - In the subject field add "Attn: Jose”
    - Link back to this thread
    - Include WP admin/network access
    - Include FTP host and credentials.

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    Thanks in advance,
    Jose

  • Jose

    Hey Monte,

    Finally found the issue. It was a tough one, but it is quite interesting.

    next_post_link() and previous_post_link() are relaying in the method get_adjacent_post().
    After struggling for a while with the results returned by this method, I found that it will fail if the post_date is not unique for each post.

    In this case, we have 18 post in the category sparklers but only 3 different timestamps (I guess you used some kind of importer and that's why you have several posts created in the same second):

    Array
    (
        [0] => stdClass Object
            (
                [ID] => 6856
                [post_date] => 2015-02-24 22:23:56
            )
        [1] => stdClass Object
            (
                [ID] => 6858
                [post_date] => 2015-02-24 22:23:56
            )
        [2] => stdClass Object
            (
                [ID] => 6844
                [post_date] => 2015-02-24 22:23:56
            )
        [3] => stdClass Object
            (
                [ID] => 6846
                [post_date] => 2015-02-24 22:23:56
            )
        [4] => stdClass Object
            (
                [ID] => 6850
                [post_date] => 2015-02-24 22:23:56
            )
        [5] => stdClass Object
            (
                [ID] => 6852
                [post_date] => 2015-02-24 22:23:56
            )
        [6] => stdClass Object
            (
                [ID] => 6854
                [post_date] => 2015-02-24 22:23:56
            )
        [7] => stdClass Object
            (
                [ID] => 6836
                [post_date] => 2015-02-24 22:23:57
            )
        [8] => stdClass Object
            (
                [ID] => 6838
                [post_date] => 2015-02-24 22:23:57
            )
        [9] => stdClass Object
            (
                [ID] => 6840
                [post_date] => 2015-02-24 22:23:57
            )
        [10] => stdClass Object
            (
                [ID] => 6842
                [post_date] => 2015-02-24 22:23:57
            )
        [11] => stdClass Object
            (
                [ID] => 6848
                [post_date] => 2015-02-24 22:23:57
            )
        [12] => stdClass Object
            (
                [ID] => 6830
                [post_date] => 2015-02-24 22:23:57
            )
        [13] => stdClass Object
            (
                [ID] => 6832
                [post_date] => 2015-02-24 22:23:57
            )
        [14] => stdClass Object
            (
                [ID] => 6834
                [post_date] => 2015-02-24 22:23:57
            )
        [15] => stdClass Object
            (
                [ID] => 6824
                [post_date] => 2015-02-24 22:23:58
            )
        [16] => stdClass Object
            (
                [ID] => 6826
                [post_date] => 2015-02-24 22:23:58
            )
        [17] => stdClass Object
            (
                [ID] => 6828
                [post_date] => 2015-02-24 22:23:58
            )
    )

    Once I spotted the issue, it was easy to find the bug report, which is in deed interesting given that it was reported ages ago but there is no a good fix yet:
    https://core.trac.wordpress.org/ticket/8107?cversion=0&cnum_hist=15
    https://core.trac.wordpress.org/ticket/26937

    That said, you have two options here:
    1- To change the post_date value in the database to ensure that all your products have a unique timestamp.
    2- To implement a custom function to get the links ordered by ID instead of post_date.
    Here is a snipped that I coded for you as a base if you want to go with the second option (there are some details to improve, like checking if there are next/prev posts available):

    global $wpdb;
     $post_id = get_the_ID();
    
     $next_sql_query = "SELECT p.ID FROM wp_posts AS p  INNER JOIN wp_term_relationships AS tr ON p.ID = tr.object_id INNER JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE p.ID > '{$post_id}' AND p.post_type = 'product' AND p.post_status = 'publish' AND tt.taxonomy = 'product_category' AND tt.term_id IN (30) ORDER BY p.ID ASC LIMIT 1";
     $next_post_id = $wpdb->get_var($next_sql_query);
    $next_link = '<a href="' . get_permalink( $next_post_id ) . '">Next post</a>';
    
    $prev_sql_query = "SELECT p.ID FROM wp_posts AS p  INNER JOIN wp_term_relationships AS tr ON p.ID = tr.object_id INNER JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE p.ID < '{$post_id}' AND p.post_type = 'product' AND p.post_status = 'publish' AND tt.taxonomy = 'product_category' AND tt.term_id IN (30) ORDER BY p.ID DESC LIMIT 1";
    $prev_post_id = $wpdb->get_var($prev_sql_query);
    $prev_link = '<a href="' . get_permalink( $prev_post_id ) . '">Prev post</a>';
    
    echo $prev_link;
    echo $next_link;

    I would suggest to go with the first option because the repeated post_date may cause other issues as well. Also, the built-in methods uses some non persistent cache to improve performance.

    Please let me know if you need further support.

    Cheers,
    Jose

  • Monte

    For future users, you can use the following SQL query in your database (phpMyAdmin, etc) to change the date

    changing the wp_3_posts to match your specific need

    UPDATE wp_3_posts SET
    post_date = CURRENT_DATE() - INTERVAL ID MINUTE,
    post_date_gmt = CURRENT_DATE() - INTERVAL ID MINUTE,
    post_modified = CURRENT_DATE() - INTERVAL ID MINUTE,
    post_modified_gmt = CURRENT_DATE() - INTERVAL ID MINUTE;

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.