Magento 2 Hide Price

Basically we have to override class ‘Magento\Catalog\Pricing\Render\FinalPriceBox ‘ to achieve this. Here I am creating an extension ‘PHPCodez_Hideprice’ that will allow you to hide price from certain user groups and diaplay a text inplace of price.

1) Declare a module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Hideprice" setup_version="0.0.1" />
</config>

2) Register a module.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'PHPCodez_Hideprice',
__DIR__
);

3) Create system.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="phpcodez" translate="label" sortOrder="200">
<label>PHPCodez</label>
</tab>
<section id="phpcodez_hideprice" translate="label" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Hide Price</label>
<tab>phpcodez</tab>
<resource>PHPCodez_Hideprice::config_hideprice</resource>
<group id="parameters" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Settings</label>
<hide_in_single_store_mode>1</hide_in_single_store_mode>

<field id="hideprice" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Hide Price For</label>
<source_model>PHPCodez\Hideprice\Model\Config\Source\Group\Multiselect</source_model>
</field>

<field id="price_message" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Text in place of Price</label>
</field>

</group>
</section>
</system>
</config>

5) Create di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="PHPCodez\Hideprice\Pricing\Render\FinalPriceBox" />
<preference for="Magento\Catalog\Model\Product" type="PHPCodez\Hideprice\Model\Product" />
</config>

5) Create source model for customer groups

<?php
namespace PHPCodez\Hideprice\Model\Config\Source\Group;
use \Magento\Customer\Model\ResourceModel\Group\Collection;

class Multiselect implements \Magento\Framework\Option\ArrayInterface {

protected $_customerGroup;

protected $_options;

public function __construct( Collection $customerGroup ) {
$this->_customerGroup = $customerGroup;
}

public function toOptionArray() {
if (!$this->_options) {
$this->_options = $this->_customerGroup->toOptionArray();
}
return $this->_options;
}
}

6) Create Render file.

<?php

namespace PHPCodez\Hideprice\Pricing\Render;

use Magento\Catalog\Pricing\Price;
use Magento\Framework\Pricing\Render;
use Magento\Framework\Pricing\Render\PriceBox as BasePriceBox;
use Magento\Msrp\Pricing\Price\MsrpPrice;

class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox {

protected function wrapResult($html) {
$result = '';

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$hidePriceHelper = $objectManager->create('\PHPCodez\Hideprice\Helper\Data');

$disabledGroups = explode(',',$hidePriceHelper->getDisabledGroups());
$customerGroup = $hidePriceHelper->getCustomerGroup();

if(in_array($customerGroup,$disabledGroups)) {
$result = $hidePriceHelper->getPriceMessage();
} else {

$result = '<div class="price-box ' . $this->getData('css_classes') . '" ' .
'data-role="priceBox" ' .
'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
'>' . $html . '</div>';

}

return $result;

}

}

7) Create Model File

<?php
namespace PHPCodez\Hideprice\Model;

class Product extends \Magento\Catalog\Model\Product {

public function isSalable() {

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$hidePriceHelper = $objectManager->create('\PHPCodez\Hideprice\Helper\Data');

$disabledGroups = explode(',',$hidePriceHelper->getDisabledGroups());
$customerGroup = $hidePriceHelper->getCustomerGroup();

if(in_array($customerGroup,$disabledGroups)) {
return false;
} else {
return parent::isSalable();
}

}
}

8) Issue teh following commands

$ php bin/magento indexer:reindex
$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush

Leave a Reply

Your email address will not be published. Required fields are marked *