I have to rewrite the product’s permalink by changing it from the default /product/product-name into /%product-cat%/product-name. %product-cat% is a custom category. But after I rewrite it, the pagination for product review is not working. Not matter what page I click, the url is correct but the reviews are always stuck at first page. This is my first attempt on doing a permalink rewrite. Did I missed out anything or added wrong line of code thus causing the pagination query to fail?
Below is the rewrite code that I have attempt:
function product_perma_Rewrite() {
$prodCat = ['taxonomy' => 'product_cat'];
$categories = get_categories($prodCat);
$catSlug = [];
foreach($categories as $category) {
$catSlug[] = $category->slug;
}
add_rewrite_rule(
'^('.implode('|', $catSlug).')/([^/]*)/?',
'index.php?post_type=product&category=$matches[1]&product=$matches[2]',
'top'
);
flush_rewrite_rules();
}
add_action('init', 'product_perma_Rewrite');
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I managed to get it fixed with the advice from tom. This is the solution I tried.
function product_perma_Rewrite() {
$prodCat = ['taxonomy' => 'product_cat'];
$categories = get_categories($prodCat);
$catSlug = [];
foreach($categories as $category) {
$catSlug[] = $category->slug;
}
add_rewrite_rule(
'^('.implode('|', $catSlug).')/([^/]+)/comment-page-([0-9]{1,})/?$',
'index.php?post_type=product&category=$matches[1]&product=$matches[2]&cpage=$matches[3]','top'
);
add_rewrite_rule(
'^('.implode('|', $catSlug).')/([^/]*)/?',
'index.php?post_type=product&category=$matches[1]&product=$matches[2]',
'top'
);
flush_rewrite_rules();
}
add_action('init', 'product_perma_Rewrite');
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0