Jump to content

Extra Content (HTML/javascript) in page


Znuff

Recommended Posts

Hello,

I'm trying to get the logged in users' customer data and then drop it in a javascript variable using a custom hook, on the domain configuration (additional page) field.

I'm looking at the documentation but I'm sure sure where to start from - all looking at the hook list, there doesn't seem to be any hook firing on:

/cart.php?a=confdomains

Or at least I can't figure out which one does.

Could someone point me in the right direction?

Link to comment
Share on other sites

I tried ClientAreaHomepage, unfortunately that does not trigger on the cart pages.

I tried ClientAreaPageCart, but this does not seem to do any actual output.

function ch_output_client_details($vars) {

return <<<HTML
<script>
  alert('hi');
</script>
HTML;

}

add_hook("ClientAreaPageCart", 1, "ch_output_client_details");

What I'm trying to do here:

- For our local .tld, a common request is that users want to register their domain names for other people/companies, but they don't particularly want to create new user accounts for each and every domain

- Yes, I am aware that WHMCS has the ability to use a different contact for domain registration, but unfortunately this is insufficient in our use case (our .tld requires extra information based on the type of business/personal entity)

- We do have almost all the fields that are common between actual account information configured as additional fields for the .tld (like name, address, email, phone etc.)

In particular, we are trying to have pre-filled data when an existing user attempts to buy a new domain while logged on. We are also trying to avoid modifying template files as much as we can - as this would create overhead and extra work when we update it from our vendor. 

So, our idea is to simply have WHMCS output a <script> in the confdomains cart step, which contains an object with the clients user information, from which we can just populate the fields on loading of the page - we are already doing form validation via javascript on that page, so it would be a natural progression.

Any tips on where/how to do this?

Link to comment
Share on other sites

Actually disregard the above.

I figured that I would rather use a "custom page" type and generate the javascript file directly from whmcs:

<?php

use WHMCS\ClientArea;
use WHMCS\Database\Capsule;
use WHMCS\User\Client;

require __DIR__ . '/../init.php';

header("Content-type: application/json");

$ca = new ClientArea();
$ca->initPage();

// Check login status
if ($ca->isLoggedIn()) {

    $d = Client::find( $ca->getUserId() );

    $client = array(
      'firstname' => $d->firstname,
      'lastname' => $d->lastname,
      'email' => $d->email,
      'address1' => $d->address1,
      'address2' => $d->address2,
      'city' => $d->city,
      'state' => $d->state,
      'postcode' => $d->postcode,
      'country' => $d->country,
      'phonenumber' => $d->phonenumber,
      'tax_id' => $d->tax_id,
      'language' => $d->language,
    );

    print(json_encode($client, true));
} else {
    // User is not logged in
}

Now I just need to figure out how do I actually get the custom fields, as Client doesn't actually return those values. I would like to avoid an extra query if I can, but I'm not sure I will be able to, as there doesn't seem to be a model for it.

Link to comment
Share on other sites

On 05/02/2019 at 15:48, Znuff said:

I tried ClientAreaPageCart, but this does not seem to do any actual output.

<?php

function ch_output_client_details($vars) {

return <<<HTML
<script>
  alert('hi');
</script>
HTML;

}

add_hook("ClientAreaHeadOutput", 1, "ch_output_client_details");

I suppose if you were wanting to inject values into the domainadditionalfields, you could either use js in an output hook, or use ClientAreaPageCart, loop though the $domain.fields array and insert your custom values into the appropriate fields and return the array back to the template - though that would use PHP rather than JS.

Link to comment
Share on other sites

  • 4 weeks later...

Just answering for posterity - 

I ended up doing what I mentioned above, I created a simple "custom page" that just outputs the current logged in client's details:

 

<?php
require __DIR__ . '/../init.php';

use WHMCS\ClientArea;
use WHMCS\Database\Capsule;
use WHMCS\User\Client;

header("Content-type: application/json");

$ca = new ClientArea();
$ca->initPage();

// Check login status
if ($ca->isLoggedIn()) {

    $d = Client::find( $ca->getUserId() );

    $client = array(
      'firstname' => $d->firstname,
      'lastname' => $d->lastname,
      'email' => $d->email,
      'address1' => $d->address1,
      'address2' => $d->address2,
      'city' => $d->city,
      'state' => $d->state,
      'postcode' => $d->postcode,
      'country' => $d->country,
      'phonenumber' => str_replace(' ', '', $d->phonenumber),
      'tax_id' => $d->tax_id,
      'language' => $d->language,
      'companyname' => $d->companyname,
    );
    
    // Custom Fields:
    // 1 - CNP
    // 3 - Reg. Com.
    // 6 - Tip Persoana
    // 33 - IBAN
    // 34 - Banca
    $d = Capsule::table('tblcustomfieldsvalues')
      ->where('relid', $ca->getUserId())
      ->get();

    foreach ($d as $i) {
      switch ($i->fieldid) {
        case 1:
          $client['cnp'] = $i->value;
          break;
        case 3:
          $client['regcom'] = $i->value;
          break;
        case 6:
          $client['tippersoana'] = $i->value;
          break;
        case 33:
          $client['iban'] = $i->value;
          break;
        case 34:
          $client['bank'] = $i->value;
          break;
      }
    }
    print(json_encode($client, true));
} else {
    // User is not logged in
}

Then I simply just did the pre-fill of my values in the page using JavaScript/jQuery:

 

 $.ajax({
   url: "/my-custom-page.php",
   success: function (data) {
     if (!$.isEmptyObject(data)) {
       $('.cnpField').val(data['cnp']);

       $('.regComField').val(data['regcom']);
       $('.cifField').val(data['codfiscal']);
       $('.selectField').val(data['tippersoana']).change();

       if ( data['tippersoana'] === 'p' || data['tippersoana'] === 'ap' ) {
         $('.ownerField').val(data['firstname'] + ' ' + data['lastname']); 
       } else {
         $('.ownerField').val(data['companyname']);
       }

       $('.mailField').val(data['email']);                                 
       $('.address1Field').val(data['address1']);
       $('.address2Field').val(data['address2']);
       $('.phoneField').val(data['phonenumber']);
       $('.countryField').val(data['country']);
       $('.stateField').val(data['state']);
       $('.cityField').val(data['city']);

     }
   }
 });

If a non-registered user goes trough the same process, it gets an empty output, because the custom page in WHMCS takes care of auth and client details. If it's empty, it simply does nothing. 

Maybe it will be useful for someone 🙂

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