Prestashop $logged and $is_logged

Bejkrools picture Bejkrools · Sep 18, 2015 · Viewed 8.5k times · Source

I get issue with Smarty, sometimes in default code occures $is_logged

..\modules\blockuserinfo\blockuserinfo.php

[...]
public function hookDisplayTop($params)
    {
        if (!$this->active)
            return;

        $this->smarty->assign(array(
            [...]
            'is_logged' => $this->context->customer->isLogged(),
            [...]
        ));
        return $this->display(__FILE__, 'blockuserinfo.tpl');
    }
[...]

..\themes\presta-bootstrap\modules\blockuserinfo\nav.tpl

<!-- Block user information module NAV  -->
{if $is_logged}
    <div class="header_user_info">
            [...]
    </div>
{/if}

but it's no working until change var's name in .tpl on $logged. Then suddenly it works! How?

Similar situation.

..\themes\presta-bootstrap\order-opc.tpl

<!-- Shopping Cart -->

        {include file="$tpl_dir./shopping-cart.tpl"}
        <!-- End Shopping Cart -->
        {if $is_logged AND !$is_guest}
            {include file="$tpl_dir./order-address.tpl"}
        {else}
            <!-- Create account / Guest account / Login block -->
            {include file="$tpl_dir./order-opc-new-account.tpl"}
            <!-- END Create account / Guest account / Login block -->

works properly only when i replace $is_logged with $logged. Both case doesn't make errors, just get FALSE value and give unexpected resuts.

Where should i looking for reasons?

Answer

Serge P picture Serge P · Sep 18, 2015

Let's make it clear, step by step, $logged and $is_logged just Smarty variables, it means they are defined somewhere.

I'm not sure what version you use, in 1.6.0.11 in blockuserinfo.php I see defined:

this->smarty->assign(array( ... 'logged' => $this->context->customer->isLogged(),

from other side in classes/controllers/Frontcontroller.php in init() method you may see:

'is_logged' => (bool)$this->context->customer->isLogged(),

and then below:

// Deprecated $this->context->smarty->assign(array( ... 'logged' => $this->context->customer->isLogged(),

Two conclusions follow:

  1. In global code better to use $is_logged, $logged may be deleted in next versions.
  2. If variable redefined, it will override global value (yes, I'm Captain Obvious).

You can check it easily e.g. by replasing FrontController variables on some dummy values like 'xxx' and 'yyy' and then in your nav.tpl like {$is_logged} = {$logged}.

Hope it will helps you better understand situation.