How to hide price from non-logged in users / guest users.

In this blog post, we will see how to hide price from non-logged in users / guest users.

Many a times in an ecommerce website, we do not wish the guest users to know the price of the catalog products. The solution to this problem is rather easy. What we just need to do is first of all figure out where the product prices are shown. Then hide them from guest users.

Attached is the source files of magento module created.
Error... Unable to load download template. Search single-download-template.tpl in your plugin folder!
While we are at it we will also hide the ‘Add to Cart’ buttons everywhere (for guest users). There is no point in hiding the price everywhere and then allowing them to add product to the cart.
In magento by default price is shown on following places
• Category list page (where all the product of a category are listed).
• Product view page (where product details are shown).
• Compare pop up (where two or more products are compared side by side).

Override the default magento files.
Since in this case we only need to override view files (phtml). We need only a very basic module. For module development refer to module development series.
Price on various places is shown from price.phtml which is located at ‘/app/design/frontend/base/default/template/catalog/product/price.phtml’.

To override this we need to add the following code in default tags of our frontend layout file.

<default>
        <reference name="catalog_product_price_template">
        	<action method="addPriceBlockType"><type>simple</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
        	<action method="addPriceBlockType"><type>grouped</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
        	<action method="addPriceBlockType"><type>configurable</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
        	<action method="addPriceBlockType"><type>virtual</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
        	<action method="addPriceBlockType"><type>bundled</type><block>catalog/product_price</block><template>hideprice/price.phtml</template></action>
        </reference>
 </default>

Then copy the default ‘price.phtml’ and place it in the specified location (in our case ‘hideprice/price.phtml’). And the place the following code on top of it.

<?php 
if(!Mage::getSingleton('customer/session')->isLoggedIn()){
	echo '<span class="login_for_price"><b>Login to See Price</b></span><br>';
	return;
}
?>

This basically hides price for guest users everywhere (since we used override in default tags).
Now to hide the ‘Add to Cart’ button on category list page. Use the following code to override the default ‘/app/design/frontend/base/default/template/catalog/product/list.phtml’. Add this to your module’s layout file.
For default

<catalog_category_default>
         <reference name="product_list">
             <action method="setTemplate"><template>hideprice/list.phtml</template></action>
         </reference>
</catalog_category_default>
And for layered navigation
<catalog_category_layered>
         <reference name="product_list">
             <action method="setTemplate"><template>hideprice/list.phtml</template></action>
         </reference>
</catalog_category_layered>

Now copy the default ‘list.phtml’ to your module. Replace the following code (around line number-109)

<?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>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>

With this.

                <?php 
                    	if(!Mage::getSingleton('customer/session')->isLoggedIn()){
	                    	echo '<span class="login_for_details" style="float:left"><b>Login to Add to Cart</b></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>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <?php 	}?>

In the above code we just check if the user is logged in. If yes the code runs normally like the default else we display a message accordingly.
Now to hide the ‘Add to Cart’ button on product detail page. Use the following code to override the default ‘/app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml’. Add this to your modules layout file.

     <catalog_product_view>
         <reference name="product.info.addtocart">
     	    <action method="setTemplate"><template>hideprice/addtocart.phtml</template></action>
     	</reference>
     </catalog_product_view>

Then copy the default ‘addtocart.phtml’ to your module and add the following code on top of it.

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

Similarly to hide the ‘Add to Cart’ on compare page. Default page is at ‘/app/design/frontend/base/default/template/catalog/product/compare/list.phtml’. We use following code in our module’s layout xml file.

     <catalog_product_compare_index>
         <reference name="catalog.compare.list">
             <action method="setTemplate"><template>hideprice/compare/list.phtml</template></action>
         </reference>
     </catalog_product_compare_index>

Then copy the default ‘compare/list.phtml’. look for the following code, it is found in two places line numbers (67 & 118)

                    <?php if($_item->isSaleable()): ?>
                        <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setPLocation('<?php echo $this->helper('catalog/product_compare')->getAddToCartUrl($_item) ?>', true)"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

Replace it with

                <?php 
                    	if(!Mage::getSingleton('customer/session')->isLoggedIn()){
	                    	echo '<span class="login_for_details" style="float:left"><b>Login to Add to Cart</b></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>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <?php 	}?>

Another Way to Hide Price for Not Logged in User (Using Event to Set Price Equals to Zero) :

To do this We use two events :-

 <catalog_product_get_final_price>
	        <observers>
	          <hideprice_catalog_price_observer>
	            <type>singleton</type>
	            <class>Wsc_Postcode_Model_Observer</class>
	            <method>hidePrice</method>
	          </hideprice_catalog_price_observer>
	        </observers>
	      </catalog_product_get_final_price>

And,

     
	       <catalog_product_collection_load_after>
	        <observers>
	          <hprice_catalog_price_observer>
	            <type>singleton</type>
	            <class>Wsc_Postcode_Model_Observer</class>
	            <method>hidePriceCatalog</method>
	          </hprice_catalog_price_observer>
	        </observers>
	      </catalog_product_collection_load_after>

=> catalog_product_get_final_price event is used to set final price equal to zero on product while catalog_product_collection_load_after event is used to set final price on catalog page.

And after that we simply check if user is not logged in (Observer.php) and set final price equals to zero

public function hidePrice($observer){
            $event = $observer->getEvent();
            $product = $event->getProduct(); 
            if(!Mage::getSingleton('customer/session')->isLoggedIn()){
            	$product->setFinalPrice(0);
    			$product->setPrice(0);
            }
            
    }
    
    public function hidePriceCatalog($observer){
    	$products = $observer->getCollection();
    
    	if(!Mage::getSingleton('customer/session')->isLoggedIn()){
    		foreach( $products as $product )
    		{
    			$product->setFinalPrice(0);
    			$product->setPrice(0);
    		}
    	}
    }

And on phtml we can also check :

 if($_product->getFinalPrice() == 0 || $_product->getPrice() == 0){
    	echo '<span class="login_for_price"><b>Login to See Price</b><br></span>';
    	return;
    }

That’s all that is required to hide the ‘price’ and ‘add to cart’ button on most of the pages. This is just a basic customization it can be further enhanced to hide other details as well as to hide details from other group of users.