Redirecting PrestaShop Subcategories to External URLs: A Developer's Approach
A PrestaShop forum thread discusses how to redirect subcategories to external URLs. The original poster, MKZ Industries, wants specific subcategories (e.g., 'Motorola', 'Samsung' under 'Mobile phones') to link to the respective brand's external website.
The Challenge: Linking Subcategories Externally
The user initially seeks a solution that avoids modifying the now-deprecated blocktopmenu.php file from older PrestaShop versions. They are using PrestaShop 8.2 and find that the old solutions no longer apply.
Initial Attempts and SEO Considerations
The user considers using .htaccess redirects but expresses concern about SEO implications. Specifically, they want the actual URL (e.g., www.motorola.com) to appear when hovering over the link, believing it's beneficial for search engine optimization.
Code Modification Solution
MKZ Industries explores modifying the ps_mainmenu module. They find a function called generateCategoriesmenu within modules\ps_mainmenu\ps_mainmenu.php. They attempt to modify this function to achieve the desired redirection.
Here's the code snippet they used:
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');
}
The user reports that this modification works for the main menu bar but not for the category tree. The thread ends with the user seeking help to extend this solution to the category tree.
Key Takeaways
- Directly modifying module files is a viable (though potentially risky) approach for achieving custom functionality.
- SEO considerations, such as displaying the target URL on hover, can influence the chosen implementation method.
- The
ps_mainmenumodule controls the main menu, and modifications there may not affect other category displays.
This thread highlights a common need for PrestaShop store owners: linking to external resources from within their category structure. While the thread doesn't provide a complete solution for the category tree, it offers a starting point and reveals the challenges involved.