Skip to content

Instantly share code, notes, and snippets.

@xlawok
Created July 18, 2021 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xlawok/4c7d80a4483f5c4d4b5e03cc179c251f to your computer and use it in GitHub Desktop.
Save xlawok/4c7d80a4483f5c4d4b5e03cc179c251f to your computer and use it in GitHub Desktop.
Prestashop 1.7 custom pagination for certain categories
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
use PrestaShop\PrestaShop\Core\Product\Search\Facet;
use PrestaShop\PrestaShop\Core\Product\Search\FacetsRendererInterface;
use PrestaShop\PrestaShop\Core\Product\Search\Pagination;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchContext;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchProviderInterface;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchResult;
use PrestaShop\PrestaShop\Core\Product\Search\SortOrder;
/**
* This class is the base class for all front-end "product listing" controllers,
* like "CategoryController", that is, controllers whose primary job is
* to display a list of products and filters to make navigation easier.
*/
abstract class ProductListingFrontController extends ProductListingFrontControllerCore
{
/**
* Takes an associative array with at least the "id_product" key
* and returns an array containing all information necessary for
* rendering the product in the template.
*
* @param array $rawProduct an associative array with at least the "id_product" key
*
* @return array a product ready for templating
*/
/**
* This returns all template variables needed for rendering
* the product list, the facets, the pagination and the sort orders.
*
* @return array variables ready for templating
*/
protected function getProductSearchVariables()
{
/*
* To render the page we need to find something (a ProductSearchProviderInterface)
* that knows how to query products.
*/
// the search provider will need a context (language, shop...) to do its job
$context = $this->getProductSearchContext();
// the controller generates the query...
$query = $this->getProductSearchQuery();
// ...modules decide if they can handle it (first one that can is used)
$provider = $this->getProductSearchProviderFromModules($query);
// if no module wants to do the query, then the core feature is used
if (null === $provider) {
$provider = $this->getDefaultProductSearchProvider();
}
$id_category = (int) Tools::getValue('id_category');
if($id_category==10){
$resultsPerPage=9999999;
}
else{
$resultsPerPage = (int) Tools::getValue('resultsPerPage');
if ($resultsPerPage <= 0) {
$resultsPerPage = Configuration::get('PS_PRODUCTS_PER_PAGE');
}
}
// we need to set a few parameters from back-end preferences
$query
->setResultsPerPage($resultsPerPage)
->setPage(max((int) Tools::getValue('page'), 1))
;
// set the sort order if provided in the URL
if (($encodedSortOrder = Tools::getValue('order'))) {
$query->setSortOrder(SortOrder::newFromString(
$encodedSortOrder
));
}
// get the parameters containing the encoded facets from the URL
$encodedFacets = Tools::getValue('q');
/*
* The controller is agnostic of facets.
* It's up to the search module to use /define them.
*
* Facets are encoded in the "q" URL parameter, which is passed
* to the search provider through the query's "$encodedFacets" property.
*/
$query->setEncodedFacets($encodedFacets);
// We're ready to run the actual query!
/** @var ProductSearchResult $result */
$result = $provider->runQuery(
$context,
$query
);
if (Configuration::get('PS_CATALOG_MODE') && !Configuration::get('PS_CATALOG_MODE_WITH_PRICES')) {
$this->disablePriceControls($result);
}
// sort order is useful for template,
// add it if undefined - it should be the same one
// as for the query anyway
if (!$result->getCurrentSortOrder()) {
$result->setCurrentSortOrder($query->getSortOrder());
}
// prepare the products
$products = $this->prepareMultipleProductsForTemplate(
$result->getProducts()
);
// render the facets
if ($provider instanceof FacetsRendererInterface) {
// with the provider if it wants to
$rendered_facets = $provider->renderFacets(
$context,
$result
);
$rendered_active_filters = $provider->renderActiveFilters(
$context,
$result
);
} else {
// with the core
$rendered_facets = $this->renderFacets(
$result
);
$rendered_active_filters = $this->renderActiveFilters(
$result
);
}
$pagination = $this->getTemplateVarPagination(
$query,
$result
);
// prepare the sort orders
// note that, again, the product controller is sort-orders
// agnostic
// a module can easily add specific sort orders that it needs
// to support (e.g. sort by "energy efficiency")
$sort_orders = $this->getTemplateVarSortOrders(
$result->getAvailableSortOrders(),
$query->getSortOrder()->toString()
);
$sort_selected = false;
if (!empty($sort_orders)) {
foreach ($sort_orders as $order) {
if (isset($order['current']) && true === $order['current']) {
$sort_selected = $order['label'];
break;
}
}
}
$searchVariables = array(
'result' => $result,
'label' => $this->getListingLabel(),
'products' => $products,
'sort_orders' => $sort_orders,
'sort_selected' => $sort_selected,
'pagination' => $pagination,
'rendered_facets' => $rendered_facets,
'rendered_active_filters' => $rendered_active_filters,
'js_enabled' => $this->ajax,
'current_url' => $this->updateQueryString(array(
'q' => $result->getEncodedFacets(),
)),
);
Hook::exec('filterProductSearch', array('searchVariables' => &$searchVariables));
Hook::exec('actionProductSearchAfter', $searchVariables);
return $searchVariables;
}
private function getProductSearchProviderFromModules($query)
{
$providers = Hook::exec(
'productSearchProvider',
['query' => $query],
null,
true
);
if (!is_array($providers)) {
$providers = [];
}
foreach ($providers as $provider) {
if ($provider instanceof ProductSearchProviderInterface) {
return $provider;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment