waverider Posted December 5, 2019 Share Posted December 5, 2019 Hey, I'm trying to add a hook to prevent all premium domain registration (and show a simple message to contact us) and it seems that I don't have the necessary hook to do this. I can handle the code that checks if a given domain is premium or not (via a custom API request to my domains provider). I just don't have the proper hook to do this. Here's what I've tried: DomainValidation can not return an error: https://developers.whmcs.com/hooks-reference/domain/#domainvalidation (returned array is ignored by WHMCS, just like the docs say). I can't use "global $smarty". Probably globals are disabled and I'd leave it like that for security reasons. I can set a value in $_SSESSION and read it from template, but it seems that the value is sent delayed (if you hit CHECK on domain multiple times, it won't update the value). Any suggestions? Thanks 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted December 5, 2019 Share Posted December 5, 2019 2 minutes ago, waverider said: Any suggestions? ShoppingCartValidateDomain - the user will be in the cart when searching for domains and the hook can return an array of error message(s). 0 Quote Link to comment Share on other sites More sharing options...
waverider Posted December 5, 2019 Author Share Posted December 5, 2019 16 minutes ago, brian! said: ShoppingCartValidateDomain - the user will be in the cart when searching for domains and the hook can return an array of error message(s). Hi, Brian, I am aware of that hook, thanks. Ideally I'd be able to show a stop-message when checking for domain availability. But might use it if nothing else is available. 0 Quote Link to comment Share on other sites More sharing options...
waverider Posted December 5, 2019 Author Share Posted December 5, 2019 Another option is to implement a registrar module with just the check domain function. But I'd avoid this fat implementation, if possible. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted December 5, 2019 Share Posted December 5, 2019 10 minutes ago, waverider said: Ideally I'd be able to show a stop-message when checking for domain availability. i've previously written code using this hook to block specific words from being searched for... of course, the hook wouldn't prevent suggested domains from appearing. 0 Quote Link to comment Share on other sites More sharing options...
waverider Posted December 5, 2019 Author Share Posted December 5, 2019 7 minutes ago, brian! said: i've previously written code using this hook to block specific words from being searched for... of course, the hook wouldn't prevent suggested domains from appearing. Hmm, which hook that is? Is it ShoppingCartValidateDomain? I though this is actually called at a later stage, after the domain is already in the cart. 0 Quote Link to comment Share on other sites More sharing options...
waverider Posted December 5, 2019 Author Share Posted December 5, 2019 12 minutes ago, brian! said: i've previously written code using this hook to block specific words from being searched for... of course, the hook wouldn't prevent suggested domains from appearing. Oh, ShoppingCartValidateDomain hook runs exactly on domain check. That's excellent! Thank you, @brian! ! 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted January 30, 2020 Share Posted January 30, 2020 @brian! I was looking at implementing this but it goes over my head. looked at the example hook code but dont know how to use it to firstly: stop say .com domains for being registered and secondly apply that same domain extension from being used in the transfer OR use existing domain option. Would you be kind enough to point out how this can be achieved? 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted January 31, 2020 Share Posted January 31, 2020 19 hours ago, Manchester Web Hosting said: looked at the example hook code but dont know how to use it to firstly: stop say .com domains for being registered and secondly apply that same domain extension from being used in the transfer OR use existing domain option. for the first half, do you mean to block .com domain registrations on the register domain page (at which point I would say don't price them!).... or are you thinking more subtly and wanting when a user tries to purchase product X, that they cannot register/transfer/use a .com domain when doing so ?? that break return must be a bug in v7.9.1 because even if you use the example in the docs, it adds a <br /> after each error message. 🙄 if you were wanting to do something similar for register/transfer, and especially as you're using standard_cart, then an alternative would be a clientareapagecart hook that removed TLD(s) from the appropriate dropdown based on the current product, e.g remove .com registration with product X and .net transfer with product Y... about the only way I can think a user could get around that would be if they ordered using a URL link that included x domain in it... but a SCDV (above) would prevent that (apart from that annoying break return in the output!). 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted January 31, 2020 Share Posted January 31, 2020 (edited) 54 minutes ago, brian! said: or are you thinking more subtly and wanting when a user tries to purchase product X, that they cannot register/transfer/use a .com domain when doing so ?? This is exactluy what I am trying to understand how to do. the hook examle doesnt make sense to me at all. Simply want to stop any speific tlds that I dont want to offer from being used after they select a product and then the options come up for either register, transfer or update nameserver domain options. I know it will be simple... The <br /> insterted looks right daft. Guess its another thing they cant seem to get right! Edited January 31, 2020 by Manchester Web Hosting 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted February 1, 2020 Share Posted February 1, 2020 @brian! I dont have any experience in this but i thought it would simple enough to create and I have tried: <?php add_hook('ShoppingCartValidateDomain', 1, function($vars) { if (in_array($vars['tld'], array("mobile", "example")) return [ '<div class="alert alert-success"><div class="row"><div class="col-sm-1"><i class="fab fa-whmcs fa-3x"></i></div><div class="col-sm-11">Welcome to WHMCS!<br><small>buy the addon!</small></div></div></div>', ]; }); <?php add_hook('ShoppingCartValidateDomain', 1, function($vars['tld']) { if (in_array($vars['tld'], array("mobile", "example")) return [ '<div class="alert alert-success"><div class="row"><div class="col-sm-1"><i class="fab fa-whmcs fa-3x"></i></div><div class="col-sm-11">Welcome to WHMCS!<br><small>buy the addon!</small></div></div></div>', ]; }); Neither work. the example hook file hasnt helped me and I havent found anything else topic wise that can point me in the right direction to create this... 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted February 1, 2020 Share Posted February 1, 2020 (edited) Hi @Manchester Web Hosting, 1 hour ago, Manchester Web Hosting said: the example hook file hasn't helped me and I haven't found anything else topic wise that can point me in the right direction to create this... there are three issues with those hooks of yours - two of which are not your fault because the docs are wrong... or the docs are right and there is yet another bug in the hook point! 🙄 in no particular order, firstly, the hook doesn't return html etc correctly using standard_cart... though of course, Modern behaves "correctly" and outputs the html... either way, there is a bug somewhere in that. secondly, the docs states that TLDs don't need the period before the TLD, e.g com should be used and not .com.... in practice, it seems to be the other way around and needs .com to be used and not just com... as I said, that's either a bug in the docs or another in the hook itself. thirdly, if you're going to do the if statement that way, it should really be... if (in_array($vars['tld'], array(".mobile",".example"))) { return ['You cannot use a '.$vars['tld'].' domain with this product.',]; } fourthly, unless you use the 'domainoption' value in the if statement, and/or a PID value, then this will block all purchases of these TLDs - including directly in domain registration.... fifthly, if you're going to do this on the domain registration page (which I don't think you are, but i'll mention for others who might), you will have to ensure that these blocked TLDs are not listed as spotlight/featured or suggested TLDs, otherwise the validation is effectively bypassed and these blocked TLDs could be added to the cart. sixthly, and lastly, an alternative for the owndomain option, would be a html5 pattern in the template... effectively, you'd probably need multiple if statements in the configureproductdomain template to cover what you want to do for each product you want to do this for, e.g block .mobile for X product, block .tv and .info for Y product etc and a custom message for each. for register/transfers, until SCVD works without the silly break return, i'd still be inclined to use a ClientAreaPageCart to remove TLDs for specific products - that's real easy and just looping through the array(s) removing the unwanted listed TLDs per product. i'm not really coding today, but I am working tomorrow if you need me to go into this further. Edited February 1, 2020 by brian! 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted February 1, 2020 Share Posted February 1, 2020 @brian! thanks for the reply. Yup the docs are not exactly inspriring to say the least. Very confusing to create a basic hook. So i used this (adapted with the code you put above): <?php add_hook('ShoppingCartValidateDomain', 1, function($vars['tld']) { if (in_array($vars['tld'], array(".mobile",".example"))) { return ['You cannot use a '.$vars['tld'].' domain with this product.',]; }); and this is the result i get... If i pop in the mobile extension it basically goes through. trying to stop specific tlds from being used in the 'you need a domain name' phase. Register and transfer are easy as I can just add in the ones I will allow to register in whmcs admin. Use nameserver not so much. My attempt(s) as you can see clearly fail. Great, if you could help and provide the hook code when you get the chance would appreciate it. Thank you. Clearly you have it working (apart from the break making it look silly)... 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted February 2, 2020 Share Posted February 2, 2020 19 hours ago, Manchester Web Hosting said: Yup the docs are not exactly inspiring to say the least. Very confusing to create a basic hook. from day one, i've found that it's simpler not to trust the WHMCS documentation to be accurate (or helpful in many ways)... and when you find that it is, treat it as a bonus! 🎉 20 hours ago, Manchester Web Hosting said: trying to stop specific tlds from being used in the 'you need a domain name' phase. Register and transfer are easy as I can just add in the ones I will allow to register in whmcs admin. I thought that we were in the realms of allowing all TLD registrations in domain registrar, but blocking their use with certain products... 20 hours ago, Manchester Web Hosting said: Use nameserver not so much. My attempt(s) as you can see clearly fail. you have slightly shot yourself in the foot by using a TLD that doesn't exist, e.g .example - as they will generate a "the domain you entered is invalid" message and that has nothing to do with the hook - using .whmcs, .brian etc would generate the same message. 20 hours ago, Manchester Web Hosting said: Great, if you could help and provide the hook code when you get the chance would appreciate it. Thank you. i've created many flavours of that hook, but the one you posted is basically correct - other than using $vars['tld'] in the function instead of just $vars... <?php add_hook('ShoppingCartValidateDomain', 1, function($vars) { if (in_array($vars['tld'], array(".mobile",".com"))) { return ["You cannot use a ".$vars['tld']." domain with this product.",]; } }); as written, it should block all those TLDs from use in products and direct registrations. 0 Quote Link to comment Share on other sites More sharing options...
Manchester Web Hosting Posted February 2, 2020 Share Posted February 2, 2020 1 hour ago, brian! said: as written, it should block all those TLDs from use in products and direct registrations @brian! ah so simple. Thank you for pointing that out to me that hook will work nicely with what i mean to achieve 😁 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.