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.

  • vivek

    Hello its not working on magento 1.7. .. i did not find back end configuration regarding this and its not impact on front end as well.. Can you please test and help me out from this issue. Waiting for your quick reply..Thanks

  • manmohan

    good explaination.

    • Manish Prakash

      Thanks Manmohan.

  • Manish Prakash

    Hi Pratik,

    It’s due to wrong class for observer in config.xml.

    Change Wsc_Postcode_Model_Observer to

    Excellence_HidePrice_Model_Observer

    It’ll works.
    Thanks

  • Dev K.

    I’m working on a project where products are added directly from category listing page. No need to go to product page. When i click on “addtocart” button, it goes to the product page and shows message “options required”. When i check view.phtml file, then i came to know that “getChildHtml(‘product_type_data’) ?>” is resposible for it. Hence, i want to know how i can load “getChildHtml(‘product_type_data’) ?>” on category product listing (list.phtml page). Plz help me

  • Dan Nguyen

    Please try my extension at http://www.mlx-store.com/hide-price.html

  • Tutorial Magento

    Hi Manish, thanks for sharing this great tutorial, many of our readers are also interested in this guide.
    May I share your tutorial at our website http://tutorialmagento.com/ ?

    Of cource, Copyright and author bio will be kept.
    Thanks

  • Maira Kotsovoulou

    Hi Manish!!! Thanks for sharing this excellent work…!!! I am a beginner in magento programming and I need some extra info… Where in the “To override this we need to add the following code in default tags of our frontend layout file” ? And what is its name…?

    I read the tutorial for module development… :-)

  • Omar

    i installed this extension only add to cart is removed but price is still displayed can u tell me why ? i need to hide the price basically so guide according to that

  • Luciano

    Hi Manish!, i’ve a few questions about how to create a magento website with membership, 3 types of memberships indeed, two of these are paid monthly by recurrent payment and the customer receives a package in his home every month, and the third type will be a free membership allowing the customer only to buy and pay with a payment type, and will be suscribed to a newsletter, i know that i must use recurring payments, but im having a headache with the registration and the customer group (the 3 types) and planning all this such as Where charge the membership (customer registration or like a common product) how to make the customers group (first create the customer and then charge the membership,etc), can you help me please? give me a starting point? or maybe you have the tutorials or a piece of a tutorials for that, something where i can start?, please i’ll apreciate any useful help