Thu. Jul 4th, 2024

36. Magento. How to hide prices and “Add to cart” button for non-logged in visitors

1 min read

This tutorial will show you how to hide prices and ‘Add to Cart’ buttons for non-logged in users in Magento.

  1. Let’s hide prices for guest users everywhere.
    1. Open app/design/frontend/base/default/template/catalog/product/ folder and copy price.phtml file to the app/design/frontend/base/template/themeXXX/catalog/product/ folder, where XXX is the number of your theme, and then place the following code on top of it:
      1
      2
      3
      4
      5
      6
      <?php
      if(!Mage::getSingleton('customer/session')->isLoggedIn()){
      echo '<span class="login_for_price"><strong>Login to See Price</strong></span><br>';
      return;
      }
      ?>
  2. Now we will hide the ‘Add to Cart’ button on category list page. Open /app/design/frontend/default/themeXXX/template/catalog/product/list.phtml file and look for the following code:
    1
    2
    3
    4
    5
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>

    and replace it with:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    if(!Mage::getSingleton('customer/session')->isLoggedIn()){
    echo '<span class="login_for_details" style="float:left"><strong>Login to Add to Cart</strong></span>';
    }
    else{
    ?>
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
    <?php    }?>

    and save changes.

  3. Now we will hide the ‘Add to Cart’ button on product detail page. Open app/design/frontend/default/themeXXX/template/catalog/product/view/addtocart.phtml file.

    Add the following code on top of it:

    1
    2
    3
    4
    5
    6
    <?php
    if(!Mage::getSingleton('customer/session')->isLoggedIn()){
    echo '<span class="login_for_details" style="float:left"><strong>Login to Add to Cart</strong></span>';
    return;<
    }
    ?>

    and save changes.

  4. In order to hide the ‘Add to Cart’ button on compare page page, open /app/design/frontend/base/default/template/catalog/product/compare/list.phtml file and copy it to /app/design/frontend/default/themeXXX/template/catalog/product/compare/ folder.

    Open it and look for the following code:

    1
    2
    3
    4
    5
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>

    and replace it with:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    if(!Mage::getSingleton('customer/session')->isLoggedIn()){
    echo '<span class="login_for_details" style="float:left"><strong>Login to Add to Cart</strong></span>';
    }
    else{
    ?>
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
    <?php    }?>

    and save changes.

  5. In order to hide the ‘Add to Cart’ button on Specials page, open /app/design/frontend/default/themeXXX/template/catalog/product/widget/sale/sale_default_list.phtml file and look for the following code:
    1
    2
    3
    4
    5
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>

    and replace it with:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    if(!Mage::getSingleton('customer/session')->isLoggedIn()){
    echo '<span class="login_for_details" style="float:left"><strong>Login to Add to Cart</strong></span>';
    }
    else{
    ?>
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
    <?php    }?>

    and save changes.

  6. In order to hide the ‘Add to Cart’ button for for new products, open /app/design/frontend/default/themeXXX/template/catalog/product/new.phtml file and look for the following code:
    1
    2
    3
    4
    5
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>

    and replace it with:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    if(!Mage::getSingleton('customer/session')->isLoggedIn()){
    echo '<span class="login_for_details" style="float:left"><strong>Login to Add to Cart</strong></span>';
    }
    else{
    ?>
    <?php if($_product->isSaleable()): ?>
    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><br>
    <?php else: ?>
    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
    <?php endif; ?>
    <?php    }?>

    and save changes.

  7. If you need to find out the location of the file which is used for the page or block not mentioned in this tutorial, please use Magento debug tool (Template Path Hints) following this tutorial How to use Magento debug tool (Template Path Hints).
July 2024
M T W T F S S
« Jun    
1234567
891011121314
15161718192021
22232425262728
293031