Jump to content

Only show text if client has custom client field unticked


Recommended Posts

you should be able to use the {$clientsdetails.customfieldsX} variable where X is the sort position of the CCF you want to use... or loop through the {$clientsdetails.customfields} array.

 

though watch out if you ever re-sort the client custom fields as it might make the code used link to the wrong CCF.

Link to comment
Share on other sites

What's CCF? Never heard of it before.

sorry, I was being lazy as was on my way out... CCF = Client Custom Field. :lol:

 

I've not done much code but HTML in my life. Care to explain a bit further? :) Thanks!

there are two ways to do this, either purely using Smarty in the template, or with a little help from an action hook...

 

to keep things simple, i'm going to assume that the CCF is the one at the top/first in your CCF setup page in the admin area - once the client has logged in, you should be able to add the Smarty code below to any client area / cart template.

 

this would check if the client has ticked the checkbox...

 

{if $clientsdetails.customfields1 eq 'on'}checkbox ticked{/if}

or if not ticked...

 

{if $clientsdetails.customfields1 eq ''}checkbox unticked{/if}

and then change the text to suit your needs.

 

if your intended customfield in 2nd in the list, use $clientsdetails.customfields2 etc.

 

where things get a little tricky is if you start to re-sort the CCF or add a new one above your selected CCF... this is because Smarty only receives the values of the customfields, not their names, so there's no way to identify the field, using Smarty, other than by their sort order - unless the value is always something very unique!

 

as long as you aren't adding a CCF above your selected CCF or re-sorting them, then the above code will work fine.

 

if you are, then use an action hook as that will always give you an accurate value - assuming that it's coded correctly! :idea:

Link to comment
Share on other sites

Is there any way to have this shown as a notification?

yes - and it's the usual two answers... Smarty in the template or an action hook - i'll give you both (as there might be circumstances where you would want to use either), but you only need to use one. :idea:

 

so, if we start with tweaking the notification code in header.tpl, we can change...

 

                    <div id="accountNotificationsContent" class="hidden">
                       {foreach $clientAlerts as $alert}
                           <div class="clientalert text-{$alert->getSeverity()}">{$alert->getMessage()}{if $alert->getLinkText()} <a href="{$alert->getLink()}" class="btn btn-xs btn-{$alert->getSeverity()}">{$alert->getLinkText()}</a>{/if}</div>
                       {foreachelse}
                           <div class="clientalert text-success"><i class="fa fa-check-square-o"></i> {$LANG.notificationsnone}</div>
                       {/foreach}
                   </div>

to...

 

                    <div id="accountNotificationsContent" class="hidden">
                       {foreach $clientAlerts as $alert}
                           <div class="clientalert text-{$alert->getSeverity()}">{$alert->getMessage()}{if $alert->getLinkText()} <a href="{$alert->getLink()}" class="btn btn-xs btn-{$alert->getSeverity()}">{$alert->getLinkText()}</a>{/if}</div>
                       {foreachelse}
                           {if empty ($clientsdetails.customfields1)}
                               <div class="clientalert text-success"><i class="fa fa-check-square-o"></i> {$LANG.notificationsnone}</div>
                           {/if}
                       {/foreach}
                       {if $clientsdetails.customfields1 eq 'on'}
                           <div class="clientalert text-info">Your checkbox is ticked!!</div>
                       {/if}
                   </div>

if you do that, then you'll get something like this...

pJac2X3.png

 

btw, note to WHMCS Staff... isn't there 2 'r' in 'interruptions' ? :roll:

 

so that's one way to do it... personally, if you were going to do this with notifications, i'd suggest the following action hook method is simpler because a) there's no need to edit header.tpl at all, and 2) you're querying the database to get the correct value and therefore removes the issue of adding new CCF and re-sorting them... if you're using a hook, that no longer matters. :idea:

 

for the hook, create a new file in /includes/hooks and call it 'ccf.php' (or anything you like!) and paste the following hook code into it...

 

<?php

use WHMCS\User\Alert;
use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAlert', 1, function($client) {

   $ccf = Capsule::table('tblcustomfieldsvalues')
               ->where('relid',$client->id)
               ->where('fieldid','8')
               ->pluck('value');

   if ($ccf == "on") {
       return new Alert('Your tickbox is checked!', 'info', 'http://www.google.com', 'Panic!');
   }
});

and you should see something like...

xBIHzQe.png

 

obviously, as I don't know the context in which your using the checkbox, i'm just giving you rough solutions - it can be fine-tuned considerably depending upon your needs.

 

there are two things to be aware of - you'll need to change one immediately, and the other at some future point.

 

the value of '8' in the code below will need to be changed for your specific needs...

 

                ->where('fieldid','8')

what you'll need to do is get the 'id' value from your 'tblcustomfields' database table for the CCF you want to query on... phpmyadmin would help you to do that.

 

41LcMgQ.png

 

I did think about doing a table 'join' but it's not really worth it - you could have multiple CCF called the same thing, so it's simpler just to get the specific ID yourself and add it to the hook code directly.

 

the other line you'll probably need to edit at some point is...

 

                ->pluck('value');

at the time i'm writing this, the above will work fine for v6.3.1 - but if/when you upgrade to v7, you'll probably need to change it to...

 

                ->value('value');

but don't worry about that until you upgrade to v7.

Edited by brian!
Link to comment
Share on other sites

just one minor tweak to the code, I just re-read your opening question and you want to do something if the checkbox is unticked - in that case...

 

    if (empty($ccf)) {
       return new Alert('Your tickbox is unchecked!', 'info', 'http://www.google.com', 'Panic!');
   }

Link to comment
Share on other sites

Do you know any good way to learn more about hooks? I really want to learn more, but it just seems to confusing...

I suspect the easiest way is to read the documentation and write some - start simple and work your way up!

 

maybe begin with the navigation hooks - navbar, sidebar and homepagepanels... though just to make that more of a challenge, some of the example hooks in the documentation aren't great, but they'd be plenty of examples here in the forum of working nav hooks which you should be able to take apart and figure out.

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