DennisHermannsen Posted June 13, 2019 Share Posted June 13, 2019 (edited) Hi, We're trying to change where products in the sidebar in the cart-menu links to. I've tried this so far without luck: <?php use WHMCS\View\Menu\Item as MenuItem; add_hook('ClientAreaSecondarySidebar', 1, function($secondarySidebar) { $secondarySidebar->getChild('Categories')->getChild('Webhosting')->setUri('webhosting.php'); }); Any idea if it's possible at all? I can remove the category-section just fine using 'removeChild('Categories')'. Edit: I obviously made a typo. That has been fixed now - but now I see an error on every other page like " Error: Call to a member function getChild() on null in " Edit2: I might have fixed it by doing this: add_hook('ClientAreaSecondarySidebar', 1, function($secondarySidebar) { if (!is_null($secondarySidebar->getChild('Categories'))) { $secondarySidebar->getChild('Categories')->getChild('Webhosting')->setUri('webhosting.php'); } }); Is that the correct way to do it? Edited June 13, 2019 by DennisMidjord 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted June 13, 2019 Share Posted June 13, 2019 Hi Dennis, 2 hours ago, DennisMidjord said: Edit: I obviously made a typo. That has been fixed now - but now I see an error on every other page like " Error: Call to a member function getChild() on null in " the golden rule when modifying a sidebar/navbar child is to first check that it exists, and only if it does should you attempt to modify it... so most of the cart pages, that hook will work because what you're trying to modify exists... go to a page outside the cart, and you're then trying to modify something that doesn't exist and WHMCS will throw an error. 2 hours ago, DennisMidjord said: Edit2: I might have fixed it by doing this - Is that the correct way to do it? it's closer and should work - unless you were to do something daft to break it like renaming that product group, or running another hook that ran before this which removed that child you're modifying... the cleanest way might be a variation of the hook I posted in the thread below... <?php # Cart Redirect Category URLs Hook # Written by brian! use WHMCS\View\Menu\Item as MenuItem; add_hook('ClientAreaSecondarySidebar', 1, function(MenuItem $secondarySidebar) { $CategoriesSidebar = $secondarySidebar->getChild("Categories"); if (empty($CategoriesSidebar)) { return; } $CategoriesSidebarChildren = $CategoriesSidebar->getChildren(); $ChildrenToRemove = array('Webhosting'=>'webhosting.php'); foreach($CategoriesSidebarChildren as $CategoryKey => $CategoryChild) { foreach($ChildrenToRemove as $label => $child) { if ($CategoryKey == $label) { $CategoriesSidebar->getChild($CategoryKey) ->setUri($child); } } } }); it's a little long-winded for just redirecting one group, but it was originally written to assign icons to multiple product groups, and so can be expanded to redirect multiple groups, just by expanding the $ChildrenToRemove array. the trick with this hook is that you're looping through the array of children in that sidebar, so that immediately removes the need to check whether they exist - we already know that they do... renaming product groups in WHMCS settings shouldn't break this hook, it would simply cause the group to be linked as normal in the cart and not redirected. 1 Quote Link to comment Share on other sites More sharing options...
DennisHermannsen Posted June 13, 2019 Author Share Posted June 13, 2019 Thanks for the great answer, @brian! 🙂 I'm gonna stick with the "simple" solution for now (as it seems to work just fine, and I don't plan to mess with the name of the 'Categories' item :P), but I'll definitely save your post for later! 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.