Username Variable

Does anybody know what the username variable is when a customer is logged in?



I have some javascript code added in a hook in the footer of all pages, and I need to insert the logged-in customer’s “username” in it. I tried “$auth.user_id”, but that didn’t work.



Is it even possible to call that variable for the javascript code to use?



Thanks.

The email (or username) is not part of the 'auth' session information. You would have to add that as a hook in the php hook 'fill_auth'. You would then be able to reference it from the $auth template variable.

I just need their variable name, so I could insert it in my JavaScript code. It doesn't need to be from a specific session, does it? How does cs-cart call it when inserting it in one of their pages? Or do they only make it available when in the “My profile details” page, for example?



Thanks.

The $auth variable is a copy of what's in the session. The email address/username (depending on the site config) is not part of the authentication info since it's only used at login and not stored.



You can write a quick function in my_changes addon like:


function my_user_email() {
if( !empty($_SESSION['auth']['user_id']) )
return db_get_field("SELECT email FROM ?:users WHERE user_id=?i", $_SESSION['auth']['user_id']);
return '';'
}


And then in your template use:


{assign var="username" value=`""|my_user_email`}
{if $username} {** found email **}
{* do whatever you want with the $username *}
{/if}

[quote name='tbirnseth' timestamp='1390420775' post='175817']

The $auth variable is a copy of what's in the session. The email address/username (depending on the site config) is not part of the authentication info since it's only used at login and not stored.



You can write a quick function in my_changes addon like:


function my_user_email() {
if( !empty($_SESSION['auth']['user_id']) )
return db_get_field("SELECT email FROM ?:users WHERE user_id=?i", $_SESSION['auth']['user_id']);
return '';'
}


And then in your template use:


{assign var="username" value=`""|my_user_email`}
{if $username} {** found email **}
{* do whatever you want with the $username *}
{/if}


[/quote]



Great! Thanks so much for your help. I will try it tonight and let you know how it worked.




[quote name='tbirnseth' timestamp='1390420775' post='175817']

The $auth variable is a copy of what's in the session. The email address/username (depending on the site config) is not part of the authentication info since it's only used at login and not stored.



You can write a quick function in my_changes addon like:


function my_user_email() {
if( !empty($_SESSION['auth']['user_id']) )
return db_get_field("SELECT email FROM ?:users WHERE user_id=?i", $_SESSION['auth']['user_id']);
return '';'
}


And then in your template use:


{assign var="username" value=`""|my_user_email`}
{if $username} {** found email **}
{* do whatever you want with the $username *}
{/if}


[/quote]



Hi again. I've been looking around but I'm really not sure where to put the function in my_changes? Usually the hook I put in footer.overide.tpl file (for example), but never played with functions before.

Create a func.php in the addon/my_changes directory and put the PHP function in that file (use opening/closing PHP tags).

[quote name='tbirnseth' timestamp='1390458681' post='175839']

Create a func.php in the addon/my_changes directory and put the PHP function in that file (use opening/closing PHP tags).

[/quote]



I'm getting closer. When I use [color=#008800][size=2]""|my_user_email [/size][/color]l get a big error on the website. So I changed the single quotes to ' ← this type of quote instead and no error. But when I do the following code:
{assign var="username" value='""|my_user_email'}
{if $username}
{$username = $username}
{$username} {** this line for testing only **}
{else}
{$username = 'guest'}
{/if}




I displays “[color=#000000][font=monospace][size=1]""|my_user_email” [/size][/font][/color]in the source code, not the username. It doesn't seem to call the function (which I put in addons/my_changes/func.php)



By the way, do you do custom changes for cs-cart, or just the ones on your website?

They aren't single-quotes, they are back-quotes. Just delete them and see if it works. Would be helpful if you posted the error message you're getting from Smarty rather than my trying to guess.

[quote name='tbirnseth' timestamp='1390511881' post='175908']

They aren't single-quotes, they are back-quotes. Just delete them and see if it works. Would be helpful if you posted the error message you're getting from Smarty rather than my trying to guess.

[/quote]



Sorry, here is the error message:


[quote]Fatal error[color=#000000][font=Times][size=1]: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template “/home/fishtail/[/size][/font][/color][color=#000000][font=Times][size=1]public_html/design/themes/basic/templates/addons/my_changes/hooks/index/piwikfooter.override.tpl” on line 1 “{if $runtime.customization_mode.design == "Y" && $smarty.const.AREA == "C"}{capture name="template_content"}{assign var="username" value=""|my_user_email}” - Unexpected ""' in /home/fishtail/public_html/app/lib/other/smarty/sysplugins/smarty_internal_templatecompilerbase.php:665 Stack trace: #0 /home/[/size][/font][/color][color=#000000][font=Times][size=1]fishtail[/size][/font][/color][color=#000000][font=Times][size=1]/public_html/app/lib/other/smarty/sysplugins/smarty_internal_templateparser.php(3144): Smarty_Internal_TemplateCompilerBase->trigger_template_error() #1 /home/[/size][/font][/color][color=#000000][font=Times][size=1]fishtail[/size][/font][/color][color=#000000][font=Times][size=1]/public_html/app/lib/other/smarty/sysplugins/smarty_internal_templateparser.php(3209): Smarty_Internal_Templateparser->yy_syntax_error(79, '') #2 /home/[/size][/font][/color][color=#000000][font=Times][size=1]fishtail[/size][/font][/color][color=#000000][font=Times][size=1]/public_html/app/lib/other/smarty/sysplugins/smarty_internal_smartytemplatecompiler.php(105): Smarty_Internal_Temp in[/size][/font][/color]/home/fishtail/public_html/app/lib/other/smarty/sysplugins/smarty_internal_templatecompilerbase.php[color=#000000][font=Times][size=1] on line [/size][/font][/color]665[/quote]



I tried deleting the back quotes and get a very similar error: