Jump to content

Hook to adjust country list?


akust0m

Recommended Posts

Hi there,

I'd like to only allow one country option instead of all of the country options available in 'includes/countries.php'.

I'd prefer a way to adjust this that will survive WHMCS updates. Is there a way to adjust the $countries array via a hook? Which hook point would I need to attach it to?

Thanks!

Link to comment
Share on other sites

14 minutes ago, akust0m said:

I'd like to only allow one country option instead of all of the country options available in 'includes/countries.php'.

if you're using v7 or later then that file is no longer used...

https://docs.whmcs.com/Customising_Countries_and_Calling_Codes#Removing_a_Country

15 minutes ago, akust0m said:

I'd prefer a way to adjust this that will survive WHMCS updates.

the above method should do that.

15 minutes ago, akust0m said:

Is there a way to adjust the $countries array via a hook? Which hook point would I need to attach it to?

I daresay you could if you had to, but the above method would be simpler. :idea:

Link to comment
Share on other sites

1 hour ago, akust0m said:

Would be good if there was a way to specify the one country I want to allow instead disabling all-1.

instinctively, I think the safer method (e.g don't give WHMCS a reason to go wrong lol) would be to remove them as per above... but I can see that would be time-consuming, so if you really want to hardcode the countries array just to be one (or more) countries, then you could use the following hook...

<?php

/**
* Only Allow Specific Countries During Cart Ordering / Registration
* @author brian!
*/
 
function cart_only_allow_specific_countries($vars)
{
	$allowed = ['United Kingdom'];
	
	if ($vars['templatefile']=='viewcart'){
		$countries = $vars['countries'];
	}
	elseif ($vars['templatefile']=='clientregister'){
		$countries = $vars['clientcountries'];
	}
		foreach ($countries as $k => $country) {
			if (!in_array($country,$allowed)) {
				unset($countries[$k]);
			}
		} 
	if ($vars['templatefile']=='viewcart'){
		return array("countries" => $countries);
	}
	elseif ($vars['templatefile']=='clientregister'){
		return array("clientcountries" => $countries);
	}	
}
add_hook("ClientAreaPage", 1, "cart_only_allow_specific_countries");

so the above will code the checkout / registration pages to remove countries other than "United Kingdom"...

4bC72mO.png

I suppose you could pull the allowed country from your localisation -> default country value instead of declaring it in the hook, but with no idea what you want to use this for, I just declared it as an array in the hook...

also, you might need to expand it for the clientregister page, but that is just a HTML string variable not an array (unlike the other two pages), so you could just code the string in the hook and return it... the complicating factor would be if the client current stored country value was different from the "allowed" country value and then that get more complicated - you could check in the hook for that (it's a basic if statement), but there's no point in coding that unless it becomes necessary. :idea:

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