Create a gist now

Instantly share code, notes, and snippets.

Embed
WooCommerce shortcode pagination on pages
<?php
/**
* Thanks for James Kemp / Iconic turning the idea into a plugin
* https://wordpress.org/plugins/shortcode-pagination-for-woocommerce
*
* This code shows pagination for WooCommerce shortcodes when it's embeded on single pages.
* Include into functions.php.
*/
if ( ! is_admin() ) {
// ---------------------- FRONTPAGE -------------------
if ( defined('WC_VERSION') ) {
// ---------------------- WooCommerce active -------------------
/**
* Set Pagination for shortcodes custom loop on single-pages.
* @uses $woocommerce_loop;
*/
add_action( 'pre_get_posts', 'kli_wc_pre_get_posts_query' );
function kli_wc_pre_get_posts_query( $query ) {
global $woocommerce_loop;
// Get paged from main query only
// ! frontpage missing the post_type
if ( is_main_query() && ( $query->query['post_type'] == 'product' ) || ! isset( $query->query['post_type'] ) ){
if ( isset($query->query['paged']) ){
$woocommerce_loop['paged'] = $query->query['paged'];
}
}
if ( ! $query->is_post_type_archive || $query->query['post_type'] !== 'product' ){
return;
}
$query->is_paged = true;
$query->query['paged'] = $woocommerce_loop['paged'];
$query->query_vars['paged'] = $woocommerce_loop['paged'];
}
/** Prepare Pagination data for shortcodes on pages
* @uses $woocommerce_loop;
*/
add_action( 'loop_end', 'kli_query_loop_end' );
function kli_query_loop_end( $query ) {
if ( ! $query->is_post_type_archive || $query->query['post_type'] !== 'product' ){
return;
}
// Cache data for pagination
global $woocommerce_loop;
$woocommerce_loop['pagination']['paged'] = $woocommerce_loop['paged'];
$woocommerce_loop['pagination']['found_posts'] = $query->found_posts;
$woocommerce_loop['pagination']['max_num_pages'] = $query->max_num_pages;
$woocommerce_loop['pagination']['post_count'] = $query->post_count;
$woocommerce_loop['pagination']['current_post'] = $query->current_post;
}
/**
* Pagination for shortcodes on single-pages
* @uses $woocommerce_loop;
*/
add_action( 'woocommerce_after_template_part', 'kli_wc_shortcode_pagination' );
function kli_wc_shortcode_pagination( $template_name ) {
if ( ! ( $template_name === 'loop/loop-end.php' && is_page() ) ){
return;
}
global $wp_query, $woocommerce_loop;
if ( ! isset( $woocommerce_loop['pagination'] ) ){
return;
}
$wp_query->query_vars['paged'] = $woocommerce_loop['pagination']['paged'];
$wp_query->query['paged'] = $woocommerce_loop['pagination']['paged'];
$wp_query->max_num_pages = $woocommerce_loop['pagination']['max_num_pages'];
$wp_query->found_posts = $woocommerce_loop['pagination']['found_posts'];
$wp_query->post_count = $woocommerce_loop['pagination']['post_count'];
$wp_query->current_post = $woocommerce_loop['pagination']['current_post'];
// Custom pagination function or default woocommerce_pagination()
kli_woocommerce_pagination();
}
/**
* Custom pagination for WooCommerce instead the default woocommerce_pagination()
* @uses plugin Prime Strategy Page Navi, but added is_singular() on #line16
*/
remove_action('woocommerce_after_shop_loop', 'woocommerce_pagination', 10);
add_action( 'woocommerce_after_shop_loop', 'kli_woocommerce_pagination', 10);
function kli_woocommerce_pagination() {
page_navi();
}
}// END WOOCOMMERCE
}// END FRONTPAGE
@hassan9

This comment has been minimized.

Show comment
Hide comment
@hassan9

hassan9 Feb 2, 2014

Hello klihelp
Great effort here , put I can not find (wc-pagination-functions.php) to test your code,,, any help please ...

hassan9 commented Feb 2, 2014

Hello klihelp
Great effort here , put I can not find (wc-pagination-functions.php) to test your code,,, any help please ...

@klihelp

This comment has been minimized.

Show comment
Hide comment
@klihelp

klihelp Mar 12, 2014

Thanks @hassan9,

Use this code in you theme functions.php.
TO make it easy, I will put this together into a plugin.
The page_navi() function is from http://wordpress.org/plugins/prime-strategy-page-navi/

Owner

klihelp commented Mar 12, 2014

Thanks @hassan9,

Use this code in you theme functions.php.
TO make it easy, I will put this together into a plugin.
The page_navi() function is from http://wordpress.org/plugins/prime-strategy-page-navi/

@christyeller

This comment has been minimized.

Show comment
Hide comment
@christyeller

christyeller Mar 14, 2014

I would love to see this as a plugin- there are so many times it just won't work to simply call the category- I often need to include details about ordering or shipping on the page, and can't do that on the category page.

BUT the code above didn't work for me :(

I would love to see this as a plugin- there are so many times it just won't work to simply call the category- I often need to include details about ordering or shipping on the page, and can't do that on the category page.

BUT the code above didn't work for me :(

@yawalkar

This comment has been minimized.

Show comment
Hide comment
@yawalkar

yawalkar Mar 19, 2014

Change line no. 10 -

from -
if ( defined('WC_VERSION') {
to
if ( defined('WC_VERSION') ) {

And, replace the function "page_navi" with "woocommerce_pagination" on line no. 96

Works good!

Change line no. 10 -

from -
if ( defined('WC_VERSION') {
to
if ( defined('WC_VERSION') ) {

And, replace the function "page_navi" with "woocommerce_pagination" on line no. 96

Works good!

@ahmeth

This comment has been minimized.

Show comment
Hide comment
@ahmeth

ahmeth Mar 26, 2014

I though i found the solution for my problem but for whatever reason I cannot get the code above with all the mods to work.

I have added the code to function.php with modification outlined by yawalkar.

I get pagination when i work through shop loop but get no pagination when is use the following short code:

[product_category category="Large Displays" orderby="price" order="ASC" ]

I'm missing something.

Help!

ahmeth commented Mar 26, 2014

I though i found the solution for my problem but for whatever reason I cannot get the code above with all the mods to work.

I have added the code to function.php with modification outlined by yawalkar.

I get pagination when i work through shop loop but get no pagination when is use the following short code:

[product_category category="Large Displays" orderby="price" order="ASC" ]

I'm missing something.

Help!

@alexandreleroux

This comment has been minimized.

Show comment
Hide comment
@alexandreleroux

alexandreleroux Sep 13, 2014

Thanks... your code worked for me... salvation! This really should be included in stock WooCommerce...

Thanks... your code worked for me... salvation! This really should be included in stock WooCommerce...

@Slotty70

This comment has been minimized.

Show comment
Hide comment
@Slotty70

Slotty70 Oct 11, 2014

hmm..i can`t get it work...made the edits of yawalkar..i use wc 2.2.6

hmm..i can`t get it work...made the edits of yawalkar..i use wc 2.2.6

@danielgc

This comment has been minimized.

Show comment
Hide comment
@danielgc

danielgc Nov 3, 2014

You are a lifesaver, klihelp your code along with yawalcar modifications are working great! this should be on the plugin already! best regards, and greetings from Mexico.

danielgc commented Nov 3, 2014

You are a lifesaver, klihelp your code along with yawalcar modifications are working great! this should be on the plugin already! best regards, and greetings from Mexico.

@klihelp

This comment has been minimized.

Show comment
Hide comment
@klihelp

klihelp Jan 1, 2015

Thanks danielgc, yawalcar.
Refreshed the code.

Owner

klihelp commented Jan 1, 2015

Thanks danielgc, yawalcar.
Refreshed the code.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Jan 17, 2015

@klihelp i added the exactly same code as above in my functions.php in theme folder on top, do i need make something more to get it work?

This is my shortcode where i want pagination: [product_category category="zubehoer" per_page="12" columns="4"]
Is this possible?

I also installed your plugin and added after in page.php, is this right`?

Please help me, im looking now a very long time ago to find a working solution.

Many Thanks

//Edit:
The problem is after adding your code above, pagination works (../page/2,../page/3,../page/4) but no pagination ist shown.

ghost commented Jan 17, 2015

@klihelp i added the exactly same code as above in my functions.php in theme folder on top, do i need make something more to get it work?

This is my shortcode where i want pagination: [product_category category="zubehoer" per_page="12" columns="4"]
Is this possible?

I also installed your plugin and added after in page.php, is this right`?

Please help me, im looking now a very long time ago to find a working solution.

Many Thanks

//Edit:
The problem is after adding your code above, pagination works (../page/2,../page/3,../page/4) but no pagination ist shown.

@hannahihayes

This comment has been minimized.

Show comment
Hide comment
@hannahihayes

hannahihayes Mar 4, 2015

Hello,

Thanks for this, it helped me to solve a problem I'd been having for a long time. However, I've noticed that the code does break the WooCommerce product search (instead of calling the archive-product.php template with no-products-found loop it instead gives me my 404 template).

Is there a solution for this?

Thanks.

Hello,

Thanks for this, it helped me to solve a problem I'd been having for a long time. However, I've noticed that the code does break the WooCommerce product search (instead of calling the archive-product.php template with no-products-found loop it instead gives me my 404 template).

Is there a solution for this?

Thanks.

@milinpatel87

This comment has been minimized.

Show comment
Hide comment
@milinpatel87

milinpatel87 Apr 3, 2015

Hello klihelp,

I am facing same problem same as Bi0to facing. On home page pagination is showing but now working. It's redirecting on same url.

I tried same things with disable the plugin also.

Please help me out.

Hello klihelp,

I am facing same problem same as Bi0to facing. On home page pagination is showing but now working. It's redirecting on same url.

I tried same things with disable the plugin also.

Please help me out.

@baddot

This comment has been minimized.

Show comment
Hide comment
@baddot

baddot Jun 14, 2015

thanks this code works like a charm!

baddot commented Jun 14, 2015

thanks this code works like a charm!

@jlr7245

This comment has been minimized.

Show comment
Hide comment
@jlr7245

jlr7245 Jun 25, 2015

Hi @klihelp !

I am trying to use your code & running into the same problem as @bi0to, it seems like the code is working but the pagination isn't displaying. Is there another step I need to do before the pagination shows up?

thanks for the code!

EDIT: Never mind, the fork https://gist.github.com/alexandreleroux/ba8aba53441967638c87 of this worked perfectly for me. Thanks so much!

jlr7245 commented Jun 25, 2015

Hi @klihelp !

I am trying to use your code & running into the same problem as @bi0to, it seems like the code is working but the pagination isn't displaying. Is there another step I need to do before the pagination shows up?

thanks for the code!

EDIT: Never mind, the fork https://gist.github.com/alexandreleroux/ba8aba53441967638c87 of this worked perfectly for me. Thanks so much!

@starecml

This comment has been minimized.

Show comment
Hide comment
@starecml

starecml Jul 6, 2015

@jlr7245 - I am using your code, but pagination is still not displaying. Any ideas?:-/ Thanks a lot!

starecml commented Jul 6, 2015

@jlr7245 - I am using your code, but pagination is still not displaying. Any ideas?:-/ Thanks a lot!

@ShekharSS

This comment has been minimized.

Show comment
Hide comment
@ShekharSS

ShekharSS Jul 13, 2015

Thanks it just worked with me, for all those who are facing problem implementing the code, This might help you, you need to change "page_navi" with "woocommerce_pagination" on line no. 96 as told by yawalker, also don't copy the whole code in your theme function file, copy from line 10 to line 101 only and see the charm.

Thanks it just worked with me, for all those who are facing problem implementing the code, This might help you, you need to change "page_navi" with "woocommerce_pagination" on line no. 96 as told by yawalker, also don't copy the whole code in your theme function file, copy from line 10 to line 101 only and see the charm.

@T-bag382

This comment has been minimized.

Show comment
Hide comment
@T-bag382

T-bag382 Jul 13, 2015

hello, i still dont understand please can someone write how i do this step by step ?

hello, i still dont understand please can someone write how i do this step by step ?

@frankthetank1234567

This comment has been minimized.

Show comment
Hide comment
@frankthetank1234567

frankthetank1234567 Sep 4, 2015

Thanks a lot for this. Works perfect on pages!

Is there anyway to get this working on posts as well? Whenever I put the shortcode in one of my posts the pagination is not working. I really need to get it working soon. Any help would be greatly appreciated!

Thanks a lot for this. Works perfect on pages!

Is there anyway to get this working on posts as well? Whenever I put the shortcode in one of my posts the pagination is not working. I really need to get it working soon. Any help would be greatly appreciated!

@duyngha

This comment has been minimized.

Show comment
Hide comment
@duyngha

duyngha Oct 14, 2015

It's awesome working!!! Thanks so much @klihelp. It saves my life.

duyngha commented Oct 14, 2015

It's awesome working!!! Thanks so much @klihelp. It saves my life.

@nadworks

This comment has been minimized.

Show comment
Hide comment
@nadworks

nadworks Jan 3, 2016

Ace code. Thanks!
Just wondering how I would be able to make the pagination optional. I.e. add a parameter into the shortcode for pagination=yes / pagination=no.
Sometimes the paginations are too much for a page that feat more than one list of products shortcode and subsequently multiple pagination rows.

nadworks commented Jan 3, 2016

Ace code. Thanks!
Just wondering how I would be able to make the pagination optional. I.e. add a parameter into the shortcode for pagination=yes / pagination=no.
Sometimes the paginations are too much for a page that feat more than one list of products shortcode and subsequently multiple pagination rows.

@kubik101

This comment has been minimized.

Show comment
Hide comment
@kubik101

kubik101 Sep 9, 2016

Hi.
I am using:

  • WooCommerce Version 2.6.4
  • WordPress Version 4.6.1

All I have done is inserted the above code into my functions.php file and inserted the following shortcode into my page
[sale_products per_page="3" columns="3"]

This has enabled paging to work by visiting the page with /page/2/
But no pagination navigation is showing.

How do I get the pagination navigation to show?

kubik101 commented Sep 9, 2016

Hi.
I am using:

  • WooCommerce Version 2.6.4
  • WordPress Version 4.6.1

All I have done is inserted the above code into my functions.php file and inserted the following shortcode into my page
[sale_products per_page="3" columns="3"]

This has enabled paging to work by visiting the page with /page/2/
But no pagination navigation is showing.

How do I get the pagination navigation to show?

@Nilesh5060

This comment has been minimized.

Show comment
Hide comment
@Nilesh5060

Nilesh5060 Sep 16, 2016

Thanks klihelp and yawalkar, thank you very much

Thanks klihelp and yawalkar, thank you very much

@elpuas

This comment has been minimized.

Show comment
Hide comment
@elpuas

elpuas Feb 25, 2017

Hi if i have a custom shortcut ( i use it for sort products by tag) is possible to added to your code after line 56 for example:

$woocommerce_loop['pagination']['my_shortcode'] = $query->my_shortcode;

elpuas commented Feb 25, 2017

Hi if i have a custom shortcut ( i use it for sort products by tag) is possible to added to your code after line 56 for example:

$woocommerce_loop['pagination']['my_shortcode'] = $query->my_shortcode;

@alticenterprise

This comment has been minimized.

Show comment
Hide comment
@alticenterprise

alticenterprise Jul 28, 2017

I used "Shortcode Pagination for WooCommerce" plugin, and work's pretty well.

I used "Shortcode Pagination for WooCommerce" plugin, and work's pretty well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment