blocks for user groups - help

How do I show specific blocks only to certain user groups?



ie. In a side bar, show block A only when a user is signed in, (not to non-members)

and show blocks B and C only to certain user groups.



I imagine that I just need to wrap the block.override.tpl with a smarty conditional statement, but I am not that familiar with smarty or what variables are used for user groups.



Someone must have done this before…

Loop this and get an array of groups into looping $grp_id

{foreach name=“auth.usergroup_ids” from=$auth.usergroup_ids item=“grp_id”}





Now you can copy blocks to new files. Inside add the loop and show anything only if that loops finds that particular group id, ids can be checked against cscart_usergroups table

Thank you for the help, Texas.



With the ‘my account’ block, I can hide/show the menu

(MY_SKIN/addons/my_changes/profiles/my_account_menu.override.tpl)

using a loop as described, but how would I hide the entire block

(MY_SKIN/customer/blocks/my_account.tpl)?



Is there a way to put a ‘my_account.override.tpl’ somewhere in ‘my_changes/’? Or what would be the appropriate hook to use to replace the entire block? I can’t seem to find a hook for this…

Ok, copy the tpl file to say “my_account_2.tpl”



Add the loop, see pseudo code below.



That new file should be placed in the same dir where the original is.



In the list where you select the kinds of blocks you will see a new entry with that new file name in there…



assign flag=0

loop user groups

…if user group==3 then flag=1

end loop

if flag==1 then

…show modified output of the block

end if



or



loop user groups

…if user group==3 then

…show modified output of the block

…end if

end loop

Thanks again Texas.

[quote name=‘colinw’]How do I show specific blocks only to certain user groups?



ie. In a side bar, show block A only when a user is signed in, (not to non-members)

and show blocks B and C only to certain user groups.



I imagine that I just need to wrap the block.override.tpl with a smarty conditional statement, but I am not that familiar with smarty or what variables are used for user groups.



Someone must have done this before…[/quote]



Hi Colin,



if you want to completely hide a block for a user group, you could do this:



Let’s say you want to hide the block “My account” for users which are not logged in.



Copy one of the block wrappers within the directory

skins/[your skin]/customer/blocks/wrappers

for example sidebox_general.tpl.



Rename it to e.g. sidebox_loggedin.tpl



Add:

{if $auth.user_id}

at the top



and:

{/if}

at the bottom of the file.



Then go to Admin, Design, Blocks and assign that new container to the block “My account”.

[quote name=‘Miss Marple’]Hi Colin,



if you want to completely hide a block for a user group, you could do this:



Let’s say you want to hide the block “My account” for users which are not logged in.



Copy one of the block wrappers within the directory

skins/[your skin]/customer/blocks/wrappers

for example sidebox_general.tpl.



Rename it to e.g. sidebox_loggedin.tpl



Add:

{if $auth.user_id}

at the top



and:

{/if}

at the bottom of the file.



Then go to Admin, Design, Blocks and assign that new container to the block “My account”.[/quote]



The best solution yet! Thanks Miss Marple!



What would be the code to use if you want to hide a block once a user is logged in?

Hi Colin,



make another copy of a wrapper, give it a name, and add the class “hidden” to the surrounding div.

After that div add {else} and add the original block from that file again between {else} and {/if}.

This will be shown if the user is not logged in.


```php {if $auth.user_id}


{$title}


{$content|default:" "}



{else}

{$title}


{$content|default:" "}



{/if} ```

Excellent. Thank you once again!

[quote name=‘Miss Marple’]Hi Colin,



make another copy of a wrapper, give it a name, and add the class “hidden” to the surrounding div.

After that div add {else} and add the original block from that file again between {else} and {/if}.

This will be shown if the user is not logged in.


```php {if $auth.user_id}


{$title}


{$content|default:" "}



{else}

{$title}


{$content|default:" "}



{/if} ```[/quote]

Even more concisely, use these lines
```php
{if $auth.user_id}
{assign var="hide_wrapper" value = true}
{/if}
```and then copy the sidebox_general div
```php

```

Awesome, Colin!

I’m trying to achieve the same thing with blocks only visible to members of a specific user group. I was trying to follow the instructions on this thread but I got lost in the loops, hehe. Anyone could lend a hand?



Thanks.

I’m also trying to hide a block from a user group (id=3), tried the following without success: Created a new block wrapper with code below. What am I doing wrong?



I’ve created a new block wrapper with the following, but it does not work:



{if $auth.usergroup_ids == ‘3’}


{$title}


{$content|default:" "}



{else}

{$title}


{$content|default:" "}



{/if}

Make sure your usergroup is “3”, if you hover your mouse over the relative usergroup in the backend the URL will contain the usergroup number


[quote name=‘postmanpat’]I’m also trying to hide a block from a user group (id=3), tried the following without success: Created a new block wrapper with code below. What am I doing wrong?



I’ve created a new block wrapper with the following, but it does not work:



{if $auth.usergroup_ids == ‘3’}


{$title}


{$content|default:" "}



{else}

{$title}


{$content|default:" "}



{/if}[/quote]

Try


{if $auth.usergroup_ids[2] == 3}

Tiagosa, I can't thank you enough for posting that. I have been having issues getting usergroup_id's to post on the product page, your snippet of code did it! Now I can hide the add to cart button for usergroups like suspended users. Thanks again!