I have created one custom block which I wants to show only when some user has logged in my website. Lets say the name of my block is 'sample_block' . How can I display 'sample_block' only when some user has logged in to my website?


You have 2 options.
The clean one is to include it in the customer_logged_in layout handle in the layout file.
Something like this.

    <reference name="left"> <!-- or right, header, content, footer -->
        <block type="...">....</block> <!-- your block declaration here -->

Second option, a bit dirty but it works.
Add an if statement at the top of the template that renders the block.

<?php if (Mage::getSingleton('customer/session')->isLoggedIn()) : ?>
    <!-- your html template here -->
<?php endif;?>

This second option can be implemented a little different if you have a block class for your template.
Just add the _toHtml() method.

protected function _toHtml() {
    if (!if (Mage::getSingleton('customer/session')->isLoggedIn())) {
        return '';
    return parent::_toHtml();

This makes the block appear only for logged in customer independent of the template you use for the block.

  • I tried <customer_logged_in> now nothing shows at all.
    – Black
    Jun 5 '18 at 12:36
  • Check for syntax errors.
    – Marius
    Jun 5 '18 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.