Jump to content

Help required with a hook


Recommended Posts

I am struggling (as a non-programmer) with getting a hook working.

I have local country versions of our frontend amd force the language and currency for each of those. I'm trying to update the hook that forces the currency so that its not unique for each country version of the site but cant get it working - it's probably a beginner mistake but any help would be appreciated.

A working country specific hook is:-

<?php
use WHMCS\Session;
$currencyId = 1;
if (Session::get('currency') != $currencyId) {Session::set('currency', $currencyId);}

Note that this isnt using add_hook('ClientAreaPage', deliberately as some products are only available for certain countries and this was necessary to allow Google etc to correctly index as otherwise products for the default currency are returned

What I am trying to do and isnt working is

<?php
use WHMCS\Session;
if ($WEB_ROOT = '/en_GB') {$currencyId = 1;}
if ($WEB_ROOT = '/fr_FR') {$currencyId = 2;}
and so on ...
if (Session::get('currency') != $currencyId) {Session::set('currency', $currencyId);}

Any guidance very gratefully received

Link to comment
Share on other sites

1 hour ago, pRieStaKos said:

Why not redirecting to `cart.php?currency=1` or `cart.php?currency=2` ?

Thanks pRieStaKos,

Because I want clean URLs, Google will see that as a redirect (AFAIK) so wont index, I dont want customers to be able to change currency (or see how to) and I am using custom product pages anyway as SEO for cart.php is pointless  -  but it's the if that isnt working

The custom pages are what are in the search engine indexes, ie https://www.truffiere.co.uk/el_GR/productdescription.php?pid=347

Edited by MrGettingRatherFrustrated
cant spell :-0
Link to comment
Share on other sites

26 minutes ago, Remitur said:

Look at this (free) hooks by Modulesgarden:

https://www.modulesgarden.com/products/whmcs/geolocation-hook

It does what you need (and much more: but source code is provided, so you can further customize it...)

 

Thanks for the suggestion, I did have a look at this before but for my setup it is way over-complicated and doesnt meet my needs, I've had a look at the code and as a non-developer it doesnt help me unfourtunately.

I really just  want what seems to be a fairly simple if condition in PHP working in the above hook

Link to comment
Share on other sites

On 28/11/2021 at 4:18 PM, Bigol'tastynuggets said:

@Kian made a fantastic module that I've always wanted but never gotten around to! 

https://katamaze.com/whmcs/mercury/specifications

Have a look at their demo, making whmcs seo friendly is akin to sweeping leaves on a windy day 🤪

Thanks, there is some really interesting stuff Kian has done and I may be interested in the future, however for current needs I am not willing to spend that sort of money and perhaps more to the point given his understandable position regarding WHMCS  and his intention not to maintain these going forward not a route I want to take unless his position changes (which seems highly unlikely)

Link to comment
Share on other sites

1 hour ago, MrGettingRatherFrustrated said:

Thanks for the suggestion, I did have a look at this before but for my setup it is way over-complicated and doesnt meet my needs, I've had a look at the code and as a non-developer it doesnt help me unfourtunately.

I really just  want what seems to be a fairly simple if condition in PHP working in the above hook

I have a quite similar need (I would like to set a different theme on just few custom pages).
I also digged in that code, hoping to find the way to do it, and went lost... 😞

 

Link to comment
Share on other sites

So, just to confirm the setup, you have:

  • Custom page in a directory that is country specific
    • el_GR/productdescription.php?pid=347
    • fr_FR/productdescription.php?pid=33
    • etc
  • You want to set the currency per country

So two options,

  • Set the currency within each productdescription.php file in each country folder
  • If for some reason you can't, where is $WEB_ROOT defined at and are you sure it is being set to those? 
    • If each country directory has their own productdescription.php, one method to getting the currency would be:
    • $country_currencies = ['en_GB'=>1,"zz_top"=>2,"xx-zz"=>5,"whatever"=>42]; 
      $country = baesname(__DIR__);
      $currency_id = (array_key_exists($country, $country_currencies) : $country_currencies[$country] : false );
      
      if ($currency_id)
      {
         //Currency ID found
         //Set the currency.....
         Session::set('currency', $currency_id)
      }

      All that can be stuffed in to a function in a common file that all custom pages include and feed the function the country using the same method of getting it.

Edited by steven99
Link to comment
Share on other sites

6 hours ago, steven99 said:

So, just to confirm the setup, you have:

  • Custom page in a directory that is country specific
    • el_GR/productdescription.php?pid=347
    • fr_FR/productdescription.php?pid=33
    • etc
  • You want to set the currency per country

So two options,

  • Set the currency within each productdescription.php file in each country folder
  • If for some reason you can't, where is $WEB_ROOT defined at and are you sure it is being set to those? 
    • If each country directory has their own productdescription.php, one method to getting the currency would be:
    • 
      $country_currencies = ['en_GB'=>1,"zz_top"=>2,"xx-zz"=>5,"whatever"=>42]; 
      $country = baesname(__DIR__);
      $currency_id = (array_key_exists($country, $country_currencies) : $country_currencies[$country] : false );
      
      if ($currency_id)
      {
         //Currency ID found
         //Set the currency.....
         Session::set('currency', $currency_id)
      }

      All that can be stuffed in to a function in a common file that all custom pages include and feed the function the country using the same method of getting it.

Thanks @steven99,

It probably helps if I explain why I am doing all of this first.
Primarily it is for SEO so that each country/language variant of the site, in conjunction with my custom productdescription.php page, is succesfully indexed although I do have the need for a custom invoice on some of these going forward 

It's is not a few pages it  is a full country specific version of the site (just a file level copy of the "master" site although I am using symbolic inks for a lot of the elements that are consistent such as modules etc). This is necessary primarily to get each language variant of the site indexed but alos allows me to see how each country site performs both in google indexes and traffic etc.

I already have an include in  header.tpl that uses if $WEB_ROOT and sets  the html lang and only the relevant language file is present in the /lang directory which has the effect of forcing the language shown (although it's probably not safe for me to rely on this second part for the language file remaining the case in future versions of WHMCS but that could then just be forced in the same way the currency is forced)

Currency needs to be slighly different and use a hook (from previous research and a ticket with WHMCS support) as it defaults to the WHMCS default currency, especially for web crawlers like google and bing etc where they load the site slightly differently from a real user and not all products are available in all countries/currencies (and therefore not translated into other languages and would be duplicates for google etc)

On each country variant I have a hook that sets the currency for that sub site and what I was trying to do was consolidate that into a single version of the hook that used if to determine which clone it was in and set the currency rther to avoid making mistakes when updating the sites. 

$WEB_ROOT is a WHMCS variable  which in my case returns the sub site path, ie '/en_GB' or '/fr_FR' and you can see in the debug output

So I have a working hook, modified for each sub-site which works:-

<?php
use WHMCS\Session;
$currencyId = 1;
if (Session::get('currency') != $currencyId) {Session::set('currency', $currencyId);}

and  I was trying to consolidate that into a single version that I used on all sites and used if statements against the WHMCS variable of $WEB_ROOT to then set the language (there are 27 languages in total - just 2 would be managable checking each hook on the wbsite) :-

 

<?php
use WHMCS\Session;
if ($WEB_ROOT = '/en_GB') {$currencyId = 1;}
if ($WEB_ROOT = '/fr_FR') {$currencyId = 2;}

and so on ...
if (Session::get('currency') != $currencyId) {Session::set('currency', $currencyId);}

the sub sites are not virtual directories in Apache they are at the file system level so the first part of the PHP variables PHP_SELF,  SCRIPT_NAME and REQUEST_URI is also the subsite. ie they would return /en_GB/productdescription.php?123 or /en_GB/store/cart.php etc
My amateur coding skills are not up to/have not been succesful in stripping that out and using that in the if statement (it is always the same format of the two character language code _ the two character country code).

If have also tried using  the explode as below but without success so really not sure what I am doing wrong (or maybe just not sure what I am doing! 🙂 )

$mywebroot = explode('/', $_SERVER['REQUEST_URI'])[1];

or

$uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$mywebroot = $uriSegments[1];

Regards
Rob

 

Edited by MrGettingRatherFrustrated
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