Jump to content

Help with Hooks Please


Recommended Posts

Hi there

 

Hope you can help I'm currently in the process of building our new site and having a bit of a problem with using Hooks

 

I'm trying to adjust the Sidebar menu on the order forms so under Categories where it lists the items I'm trying to use Hooks to change the URL to go somewhere else.

 

Also under the actions bit, I was trying to hide the Domain Renewals section I'm using the below code to do this

 

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function($secondarySidebar) 
{
   $secondarySidebar->getChild('Categories')
       ->getChild('Web Hosting')
       ->setUri('https://www.domain-name.uk/webhosting');
});


add_hook('ClientAreaSecondarySidebar', 1, function($secondarySidebar) 
{
   $secondarySidebar->getChild('Categories')
       ->getChild('Reseller Hosting')
       ->setUri('https://www.domain-name.uk/reseller-hosting');
});

 

Now, this does what I want it to do with no problem but when I go to the client area the page is blank it won't load if I remove the file from the Hooks folder the client area loads fine.

 

I have found a few different ways of doing it but I haven't had any luck and I hope that someone can shed some light on it as I have run out of ideas

 

or if there is a better way of doing this without using hooks maybe

 

I look forward to your reply.

Link to comment
Share on other sites

Hi John,

 

your problem is caused by the hook code not checking whether the sidebar exists before trying to modify it - so that's why it will work fine in the cart (where it does exist), and fails elsewhere (where it doesn't).

 

so the quick solution is to check that the parent sidebar exists before trying to modify it...

 

<?php

use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaSecondarySidebar', 1, function($secondarySidebar) 
{
   if (!is_null($secondarySidebar->getChild('Categories'))) {
       $secondarySidebar->getChild('Categories')
                       ->getChild('Web Hosting')
                       ->setUri('https://www.domain-name.uk/webhosting');

       $secondarySidebar->getChild('Categories')
                       ->getChild('Reseller Hosting')
                       ->setUri('https://www.domain-name.uk/reseller-hosting');
   }
});

the only way the above hook should fail would be if either of the product groups got renamed or deleted - you could code the hook to get around that if you had to, but it complicates it... so simplest solution is to remember to modify the hook if you change either of these product group names!

Link to comment
Share on other sites

Ok I have just tried this and its working fine been able to hide the Product Addons now as well without it breaking so thank you very much for your help was on it for a while last night just couldn't get my head around it

 

So again thank you for your help

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated