Redirecting PrestaShop Subcategories to External Websites: A Code Modification Approach
Adding External Links to PrestaShop Subcategories: A Developer's Dive
This insight summarizes a PrestaShop forum thread where a user, MKZ Industries, sought to redirect subcategories to external websites. The goal was to have clicks on specific subcategories, such as 'Motorola,' lead to an external URL (e.g., www.motorola.com) instead of the standard category page.
The Challenge
The user successfully modified the ps_mainmenu module to achieve this for the top menu. However, implementing the same functionality for the category tree in the left column proved difficult. The initial approach involved directly modifying the ps_categorytree module.
Initial Code Modification Attempt
MKZ Industries attempted to modify the formatCategory function within ps_categorytree.php. The original code snippet from PrestaShop 8.2 is shown below:
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 user's attempted modification involved adding a conditional statement to change the 'link' value based on the category ID. However, the syntax was incorrect.
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),
];
}
Solution and Best Practices
Another forum user, 4presta, provided a corrected code snippet, demonstrating how to properly implement the conditional link assignment:
private function formatCategory($rawCategory, $idsOfCategoriesInPath): array {
$children = [];
if (!empty($rawCategory['children'])) {
foreach ($rawCategory['children'] as $k => $v) {
$children[$k] = $this->formatCategory($v, $idsOfCategoriesInPath);
}
}
$categoryLink = '';
if ($rawCategory['id_category'] == 12) {
$categoryLink = 'https://www.motorola.com';
} elseif ($rawCategory['id_category'] == 13) {
$categoryLink = 'https://www.nokia.com';
} elseif ($rawCategory['id_category'] == 14) {
$categoryLink = 'https://www.samsung.com';
} else {
$categoryLink = $this->context->link->getCategoryLink($rawCategory['id_category'], $rawCategory['link_rewrite']);
}
return [
'id' => $rawCategory['id_category'],
'link' => $categoryLink,
'name' => $rawCategory['name'],
'desc' => $rawCategory['description'],
'children' => $children,
'in_path' => in_array($rawCategory['id_category'], $idsOfCategoriesInPath),
];
}
The user also emphasized the importance of using overrides and hooks for modifications, rather than directly altering core module files. This approach ensures easier updates and avoids potential conflicts. While the original poster acknowledged the best practice, they admitted a lack of familiarity with overrides and hooks.
Conclusion
The thread highlights a common PrestaShop customization task: redirecting subcategories to external URLs. While direct code modification can achieve this, using overrides and hooks is the recommended approach for maintainability and compatibility. This example showcases the PrestaShop community's collaborative spirit in providing solutions and promoting best practices.