Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Layout Blocks in Magento depending if logged in?

What is the easiest/most recommended way to have some conditional layout logic based on if a user is logged or not?

eg.. in pseudocode

if (customer logged in)
  display this custom onepage checkout layout block
otherwise
  display this other custom layout block (for guests)
like image 788
Zabs Avatar asked May 16 '14 14:05

Zabs


2 Answers

Use conditions in your xml file (local.xml, page.xml, etc.). E.g. to add a cms block if customer is logged in:

<customer_logged_in>
    <reference name="name_of_reference_block">
        <block type="cms/block" name="block_name">
            <action method="setBlockId">
                <block_id>block_id</block_id>
            </action>
        </block>
    </reference>
</customer_logged_in>

And same logic for logged out customers, e.g. remove a block:

<customer_logged_out>
    <reference name="name_of_reference_block">
        <remove name="name_of_block_to_remove"></remove>
    </reference>
</customer_logged_out>
like image 92
Gerard de Visser Avatar answered Oct 22 '22 01:10

Gerard de Visser


You can also use Magento customer helper in your template:

if($this->helper('customer')->isLoggedIn()){

//Show block to logged in customers

}else{

// Show different block to guest users

}

Alternatively use:

Mage::helper('customer')->isLoggedIn() 
like image 36
iamgraeme Avatar answered Oct 22 '22 01:10

iamgraeme