Jump to content

Adding an external link to subcategory


Recommended Posts

Hi all, 

I would like to add an external link to a subcategory so that when a customer clicks on that subcategory, he is redirected to another external website. Both in the main manu and in the category tree. 

Let's imagine that I have the following categories and subcategories:

Laptops
Mobile phones
- Motorola
- Samsung

The idea is that if someone clicks on ‘Motorola’, they will be taken to www.motorola.com. I have managed to do this for the top menu modifying as follows the "generateCategoriesmenu" function in "modules\ps_mainmenu\ps_mainmenu.php", but I haven't been able to do it for the category tree on the left column. 

foreach ($categories as $key => $category) {
            $node = $this->makeNode([]);
            if ($category['level_depth'] > 1) {
                $cat = new Category($category['id_category']);
                if ((int)$cat ->id_category == 12) 
                    $link = "https://www.motorola.com";
                else
                    $link = $cat->getLink();
                // Check if customer is set and check access
                if (Validate::isLoadedObject($this->context->customer) && !$cat->checkAccess($this->context->customer->id)) {
                    continue;
                }
            } else {
                $link = $this->context->link->getPageLink('index');
            } 

How could I do this? Any ideas with a different approach?

Thank you very much for your help and best regards

Link to comment
Share on other sites

On 1/29/2026 at 2:45 PM, MKZ Industries said:

The idea is that if someone clicks on ‘Motorola’, they will be taken to www.motorola.com. I have managed to do this for the top menu modifying as follows the "generateCategoriesmenu" function in "modules\ps_mainmenu\ps_mainmenu.php", but I haven't been able to do it for the category tree on the left column. 

Hi.
There is a module ps_categorytree on the left menu display.
You would also have to edit ps_categorytree.php specifically the formatCategory function

Link to comment
Share on other sites

17 hours ago, 4presta said:

Hi.
There is a module ps_categorytree on the left menu display.
You would also have to edit ps_categorytree.php specifically the formatCategory function

Yes, I have been playing around with that file and function, but I must be doing something wrong. This is the original code of that function in PS 8.2: 

private function formatCategory($rawCategory, $idsOfCategoriesInPath): array
{
	$children = [];
	if (!empty($rawCategory['children'])) {
		foreach ($rawCategory['children'] as $k => $v) {
			$children[$k] = $this->formatCategory($v, $idsOfCategoriesInPath);
		}
	}

	return [
		'id' => $rawCategory['id_category'],
		'link' => $this->context->link->getCategoryLink($rawCategory['id_category'], $rawCategory['link_rewrite']),
		'name' => $rawCategory['name'],
		'desc' => $rawCategory['description'],
		'children' => $children,
		'in_path' => in_array($rawCategory['id_category'], $idsOfCategoriesInPath),
	];
}

The idea is the same as with the main menu: add a condition so that when the category ID is 12, the link is ‘www.motorola.com’. I'm not an expert in PHP code and I may not be using the correct syntax, but it should be something like this: 

private function formatCategory($rawCategory, $idsOfCategoriesInPath): array
{
	$children = [];
	if (!empty($rawCategory['children'])) {
		foreach ($rawCategory['children'] as $k => $v) {
			$children[$k] = $this->formatCategory($v, $idsOfCategoriesInPath);
		}
	}

	return [
		'id' => $rawCategory['id_category'],
		if($rawCategory['id_category'] == 12){
			'link' = "https://www.motorola.com";
		}
		else {
			'link' => $this->context->link->getCategoryLink($rawCategory['id_category'], $rawCategory['link_rewrite']);
		},
		'name' => $rawCategory['name'],
		'desc' => $rawCategory['description'],
		'children' => $children,
		'in_path' => in_array($rawCategory['id_category'], $idsOfCategoriesInPath),
	];
}
Edited by MKZ Industries (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...