Site logo

Archived topic

"Previous" and "Next" buttons in Woocommerce

25 replies · Started by Alberto on July 31, 2018

Viewing posts 16–26 of 26

So I just added this function to my test site:

add_action( 'woocommerce_before_single_product', 'tu_prev_next_product' );
function tu_prev_next_product(){
    echo '<div class="prev_next_buttons">';
        next_post_link( '%link', '&larr; Previous', true, false, 'product_cat' );
        previous_post_link( '%link', 'Next &rarr;', true, false, 'product_cat' );
    echo '</div>';
}

And it worked: http://prntscr.com/kdwujr

I wonder if you have a plugin that's conflicting?

Tom.

I am adding the code through the Code Snippets plugin.

I do not know how you are adding your ...

Hi there,

Works fine with Code Snippets.
On your site i can see in the code that the Container is being output to the page, but the nav links are missing.

As Tom mentioned it could be a Plugin conflict, can you try disabling plugins to see if this resolves the issue. I would begin with the YITH WC accordion widget......

David.

I inform you that I have disabled the plugin YITH WooCommerce Category Accordion and the arrows still do not appear ...

Can you work through each of the plugins? Probably best to disable any caches you may have whilst testing.

Hi Tom, David

I have used the Bloomer code you linked to above, and it works. The problem is that the Next and Previous link to the alphabetical next and previous product - but only within same category.

I would like to link to the next and previous product of all the products (alphabetically as they are shown in the 'shop'). Any help gratefully received...

The key bit of the Bloomer code seems to be this:
$previous = next_post_link('%link', '← PREVIOUS', TRUE, ' ', 'product_cat');
$next = previous_post_link('%link', 'NEXT →', TRUE, ' ', 'product_cat');

I have inserted %title to get the name of the next and previous plants:
$previous = next_post_link('%link', '← %title', TRUE, ' ', 'product_cat');
$next = previous_post_link('%link', '%title →', TRUE, ' ', 'product_cat');

I have read: https://developer.wordpress.org/reference/functions/next_post_link/
and have tried FALSE to the question of being within the category.

$previous = next_post_link('%link', '← %title', FALSE);
$next = previous_post_link('%link', '%title →', FALSE);

But the result is almost random steps, where Next takes you to a product in which Previous is not the one you came from (if you get my gist!).

It ought to be simple... How can I do it???

Major thank you.

That should do it, but I know WooCommerce can act weird with post navigation. You can always var_dump($previous); to debug what the variable is returning.

May be worth opening a new topic if you can't get it working - this one is quite old.

Thanks!

Hello,

I have the same issue and I followed the same instructions with Code Snippets and CSS but it's also not working for me. Would someone be able to help, or point me in the direction of the plugin that could be blocking it?

Or shall I open a new ticket? :)

Thanks,
Katie

A new ticket would be great, especially if you could link us to your site :)

Thanks!

Hello Tom, i was also looking for this.
I tried the code that you gave and it does work for me.(magiciansandmagicshop.com).
However, i did notice something else.
If you are using more than one category, it diverts to the next of the primary category (e.g. rankmath give option of a primary category)- so although i might have a product in a primary category of magic downloads, but also have a category linked of magic books- then when scrolling through a magic book and clicking next, it diverts to the next product in magic downloads.

So, this is not ideal solution in this use case.
(i am just leaving it, as it at least allows everyone to scroll through to another product- better than not).

Hi there,

thanks for the feedback.
Unfortunately those Primary Categories that RankMath and Yoast add are not core functions.
So the core function that Tom used in his code has no handle on them.

It may be possible to filter out ALL other terms if a Primary Category is present. But from past experience i have found Woocommerce does some peculiar things with next / prev navigation. I think this would require a dedicated Woocommerce developer to resolve.

This archived topic is closed to new replies.