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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With