First Programer
First Programer

Reputation: 147

Create homepage pagination in Prestashop

I created a module named "blockallproducts" and it is displaying all products in homepage and pagination too. But how can I pass a value for paginaton page value to the module? Or do I need to create a one more controller? Anyone help me please! This is a function hookdisplayHome($params) to show all products in blockallproducts.php.

$p = 1;
$n = 10;
$pages_nb = ceil($this->countAllProducts() / (int)$n);

$range = 2; /* how many pages around page selected */
$start = (int)($p - $range);
if ($start < 1)
    $start = 1;
$stop = (int)($p + $range);
if ($stop > $pages_nb)
    $stop = (int)$pages_nb;

if (!$p) $p = 1;
    $category = new Category(Context::getContext()->shop->getCategory(),(int)Context::getContext()->language->id);
    $products = $category->getProducts((int)Context::getContext()->language->id,((int)$p - 1) * (int)$n,(int)$n,'id_product','DESC');

$this->smarty->assign(array(
  'new_products'  =>  $products,
  'homeSize'      => Image::getSize(ImageType::getFormatedName('home')),
  'pages_nb' => $pages_nb,
  'prev_p' => $p != 1 ? $p - 1 : 1,
  'next_p' => (int)$p + 1  > $pages_nb ? $pages_nb : $p + 1,
  'requestPage' => $this->context->link->getPageLink('blockallproducts'),
  'p' => $p,
  'n' => $n,
  'range' => $range,
  'start' => $start,
  'stop' => $stop,
  /*'ajaxurl'   => $this->context->link->getModuleLink('blockallproducts', 'display')*/
  ));
return $this->display(__FILE__, 'blocknewproducts_home.tpl');

Upvotes: 3

Views: 861

Answers (1)

Indrė
Indrė

Reputation: 1136

Pagination in Category controller is done using URL parameters like ?p=2 for the page number.

If you want to use pagination in Home page and keep the clean URL, you can add some javascript to next, previous buttons and your-module/controllers/front/your-controller.php to handle your requests about products data.

Upvotes: 1

Related Questions