Tutorials/Hiding elements for non registered user

From OXIDwiki

Jump to: navigation, search

A common requirement in many shops is to display certain features or elements only to registered users. This can be used to restrict features or benefits to registered users, and to motivate anonymous visitors to sign up and provide their details to the shop owner.

This tutorial discusses one technique to implement this requirement in OXID eShop, using a custom Smarty block plugin.


Step 1: Create the Smarty Plugin

Create the file /core/smarty/plugins/block.oxhideblock.php and fill it with the following code:
 * Smarty {oxhideblock}{/oxhideblock} block plugin
 * Type:     block function<br>
 * Name:     oxhideblock<br>
 * Purpose:  show specific content just for logged in users
 * @param array  $params  params
 * @param string $content contents of the block
 * @param Smarty &$smarty clever simulation of a method
 * @param bool   &$repeat repeat
 * @return string $content re-formatted
function smarty_block_oxhideblock( $params, $content, &$smarty , &$repeat)
    $userid = oxSession::getVar( 'usr' );
    if (!$repeat && !$userid) {
        return '';
    return $content;

Step 2: Use the Smarty Plugin in OXID Page Templates

To see how this works, let's take a simple example: displaying a custom message bar to logged in users. To do this, add the following code to the start page template /out/basic/tpl/start.tpl

    <strong class="head2">
        Hello [{$oxcmp_user->oxuser__oxfname->value}] [{$oxcmp_user->oxuser__oxlname->value}], 
        Welcome on board!

Once you save the changes, try accessing the OXID eShop start page before and after logging in. The following figures illustrate the difference:

Not logged in:

Hideblock noreg.jpg

Logged in:

Hideblock reg.jpg

As illustrated in the previous listing, it is quite easy to use this Smarty plugin in a template file. Simply surround the code block you wish to hide with [{oxhideblock}] ... [{/oxhideblock}] tags, and the block will only be visible to registered users. It's also quite easy to adapt this technique to different uses - for example, displaying certain content only to high-value shoppers - by modifying the business rules inside the Smarty plugin. 

Personal tools