Hide Price for Guest Users
Hello,
[Magento version 1.7]
A pretty common question we hear from Magento users and on the Magento forums is how to hide the product prices, both from the catalog and product page from guest users and show it only to logged in users. So today we will discuss how to do it and make a custom module out of it. You can download the full extension from http://aumento.ballistichq.com/extension/pricehide-hide-price-magento-catalog-page/
Now moving on to the code. The first file is the module installation file. This XML lets Magento know that we are creating a new extension and that it should look into the module perspective folders.
Module Installation File:
/app/etc/modules/Aumento_PriceHide.xml
<config> <modules> <Aumento_PriceHide> <active>true</active> <codePool>local</codePool> </Aumento_PriceHide> </modules> </config>
Two configuration files are still required, the module configuration file:
Module Configuration File:
/app/code/local/Aumento/PriceHide/etc/config.xml
<config> <modules> <Aumento_PriceHide> <version>1.0</version> </Aumento_PriceHide> </modules> <global> <blocks> <catalog> <rewrite> <product_price>Aumento_PriceHide_Block_Price</product_price> </rewrite> </catalog> </blocks> <helpers> <pricehide> <class>Aumento_PriceHide_Helper</class> </pricehide> </helpers> </global> </config>
And the admin configuration file, this file insert all the configuration fields to Magento Backend:
Module Admin Configuration File:
/app/code/local/Aumento/PriceHide/etc/system.xml
<config> <sections> <catalog> <groups> <pricehideconfig translate="label"> <label>Aumento.io Price Hide</label> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> <fields> <active translate="label"> <label>Enabled</label> <frontend_type>select</frontend_type> <source_model>adminhtml/system_config_source_yesno</source_model> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </active> <title translate="label"> <label>Message to display</label> <frontend_type>text</frontend_type> <sort_order>2</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> </title> </fields> </pricehideconfig> </groups> </catalog> </sections> </config>
Now to the actual code, we are going to override Mage_Catalog_Block_Product_Price, this Block is responsible for displaying the product prices all over magento. We simply read the module settings from the configuration tables, and then override the _toHtml function. This function is responsible for rendering the block to presentation code. We are going to check the user login state and then load our template if both the module is enabled, and the user is not logged in. If that’s not the case, we will simply load the default template.
Module Block File:
/app/code/local/Aumento/PriceHide/BlockPrice.php
class Aumento_PriceHide_Block_Price extends Mage_Catalog_Block_Product_Price {
public $config_message = '';
private $config_enabled = '';
protected function _construct() {
parent::_construct();
$this->config_message = Mage::getStoreConfig('catalog/pricehideconfig/title');
$this->config_enabled = Mage::getStoreConfig('catalog/pricehideconfig/active');
}
protected function _toHtml(){
if ( ($this->config_enabled == 1) && !($this->helper('customer')->isLoggedIn()) ) {
$this->setTemplate('pricehide/price.phtml');
}
if (!$this->getProduct() || $this->getProduct()->getCanShowPrice() === false) {
return '';
}
return parent::_toHtml();
}
}
Now comes the presentation part, a one liner phtml file that loads a custom message set in the module configuration.
Module Template File:
/app/design/frontend/package/theme/template/pricehide/price.phtml
<p> <?php echo $this->config_message; ?> </p>
Am empty Helper file is also required for the module to work. Get the full module at http://aumento.ballistichq.com/extension/pricehide-hide-price-magento-catalog-page/ . And that’s how you do it.