Change pagination base slug on WooCommerce archive product page

Viewed 595

I want to change pagination slug, currently 'page' to 'strana' on WooCommerce archive pages.

Default setting with urls http://localhost/obchod/ and http://localhost/obchod/page/2/ works fine. When I change pagination base and resave permalinks it not works. Final url should look like: http://localhost/obchod/strana/2/

This is my code for changing the pagination base:

function custom_rewrite_rules() {
  global $wp_rewrite;
  $wp_rewrite->pagination_base = 'strana';
}
add_action('init', 'custom_rewrite_rules');

I always resave permalinks on WordPress or I use $wp_rewrite->flush_rules();, but non of these works.

Note that this code works for WordPress pagination, but not for WooCommerce pagination shop pages.

What Am I doing wrong?

1 Answers

First we need to change the format of the links that Wordpress generates. As we need to change the pagination format only for the shop archive, we add a conditional tag is_shop() , so another pagination will stay in old format.

function custom_rewrite_rules() {
global $wp_rewrite;
if(is_shop()) {
$wp_rewrite->pagination_base = 'strana';
}
}
add_action('template_redirect', 'custom_rewrite_rules');

Next, we add_rewrite_rule() to make Wordpress understand the new link format for pagination pages

add_rewrite_rule(
'^obchod/strana/([^/]+)/?',
'index.php?paged=$matches[1]',
'top'
); 

obchod here = your shop base, default its shop fow woocommerce stores

Update for test

function custom_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->pagination_base = 'strana';

}
add_action('init', 'custom_rewrite_rules');

add_rewrite_rule(
'^obchod/strana/([^/]+)/?',
'index.php?paged=$matches[1]&post_type=product',
'top'
); 
Related