Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify category pagination for SEO purposes #9472

Merged
merged 1 commit into from Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 23 additions & 7 deletions classes/controller/ProductListingFrontController.php
Expand Up @@ -418,17 +418,33 @@ protected function getTemplateVarPagination(
$itemsShownFrom = ($query->getResultsPerPage() * ($query->getPage() - 1)) + 1;
$itemsShownTo = $query->getResultsPerPage() * $query->getPage();

$pages = array_map(function ($link) {
$link['url'] = $this->updateQueryString(array(
'page' => $link['page'] > 1 ? $link['page'] : null,
));

return $link;
}, $pagination->buildLinks());

//Filter next/previous link on first/last page
$pages = array_filter($pages, function ($page) use ($pagination) {
if ('previous' === $page['type'] && 1 === $pagination->getPage()) {
return false;
}
if ('next' === $page['type'] && $pagination->getPagesCount() === $pagination->getPage()) {
return false;
}

return true;
});

return array(
'total_items' => $totalItems,
'items_shown_from' => $itemsShownFrom,
'items_shown_to' => ($itemsShownTo <= $totalItems) ? $itemsShownTo : $totalItems,
'pages' => array_map(function ($link) {
$link['url'] = $this->updateQueryString(array(
'page' => $link['page'],
));

return $link;
}, $pagination->buildLinks()),
'current_page' => $pagination->getPage(),
'pages_count' => $pagination->getPagesCount(),
'pages' => $pages,
// Compare to 3 because there are the next and previous links
'should_be_displayed' => (count($pagination->buildLinks()) > 3),
);
Expand Down
17 changes: 16 additions & 1 deletion controllers/front/listing/CategoryController.php
Expand Up @@ -52,7 +52,22 @@ public function canonicalRedirection($canonicalURL = '')

public function getCanonicalURL()
{
return $this->context->link->getCategoryLink($this->category);
$canonicalUrl = $this->context->link->getCategoryLink($this->category);
$parsedUrl = parse_url($canonicalUrl);
if (isset($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $params);
} else {
$params = array();
}
$page = (int) Tools::getValue('page');
if ($page > 1) {
$params['page'] = $page;
} else {
unset($params['page']);
}
$canonicalUrl = http_build_url($parsedUrl, ['query' => http_build_query($params)]);

return $canonicalUrl;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion themes/classic/templates/_partials/pagination.tpl
Expand Up @@ -34,7 +34,8 @@
{if $pagination.should_be_displayed}
<ul class="page-list clearfix text-sm-center">
{foreach from=$pagination.pages item="page"}



<li {if $page.current} class="current" {/if}>
{if $page.type === 'spacer'}
<span class="spacer">&hellip;</span>
Expand Down