Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current page and url in prestashop

I am trying to get the url of current page in prestashop to test multiple pages of a website even if the website pages do not have a consistent pattern. I noticed that most of the prestashop pages are of the form /index.php?id_category=<categoryid>&controller=<controllername>, /index.php?id_product=<productid>&controller=<controllername> etc. I am able to get the controller name using Context::getContext()->controller->php_self

So, I want to know how do I get the product id or category id to form the current url of a page?

like image 373
Archit Verma Avatar asked Dec 24 '22 17:12

Archit Verma


1 Answers

There's no need to manually reconstruct the link, you can use the Link class.

// Context
$context = Context::getContext();
// Category id (on category and product page)
$cid = $context->controller->getCategory()->id;
// Product id (on product page)
$pid = $context->controller->getProduct()->id;

// Category link
$cat_link = $context->link->getCategoryLink($cid);
// Product link
$prod_link = $context->link->getProductLink($pid);
like image 141
gabdara Avatar answered Dec 28 '22 05:12

gabdara