There are 2 solution for what you need:
1. Use ready magento extensions
You can try these extensions
http://kamtechco.net/products/magent...extension.html
or
http://www.magentocommerce.com/magen...gistered-users
2. Do it yoursefl (in the case you are magento developer)
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.
HTML Code:
<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 Code:
<?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
HTML Code:
<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 Code:
<?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.
HTML Code:
<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 Code:
<?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.
HTML Code:
<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 Code:
<?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 Code:
<?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 :-
HTML Code:
<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,
HTML Code:
<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
PHP Code:
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 :
PHP Code:
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.
As always, I would recommend you use ready extension
Bookmarks