I got to succeed in hiding price from whole frontend area with a custom module.
I have di.xml as below:
<preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Vendor\Module\Model\Pricing\Render\FinalPriceBox" />
<preference for="Magento\Sales\Model\Order" type="Vendor\Module\Model\Sales\Order" />
<preference for="Magento\Catalog\Model\Layer\Category\FilterableAttributeList" type="Vendor\Module\Model\Layer\FilterableAttributeList" />
<preference for="Magento\Directory\Model\PriceCurrency" type="Vendor\Module\Model\Directory\PriceCurrency" />
I override below files and update functions as needed, Here I list out all the files and functions:
Hide price from Customer account section, Order view, etc.
- Vendor\Module\Model\Sales\Order.php
==>> function formatPrice($price, $addBrackets = false)
Hide price from the product, category, new products, related, upsell, cross-sell, etc.
- Vendor\Module\Model\Pricing\Render\FinalPriceBox.php
==>> function wrapResult($html)
Hide price filter from left / right navigation
- Vendor\Module\Model\Layer\FilterableAttributeList.php
==>>
class FilterableAttributeList extends \Magento\Catalog\Model\Layer\Category\FilterableAttributeList
{
protected $collectionFactory;
protected $storeManager;
protected $helperData;
public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $collectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Vendor\Module\Helper\Data $helperData
) {
$this->collectionFactory = $collectionFactory;
$this->storeManager = $storeManager;
parent::__construct($collectionFactory, $storeManager);
$this->helperData = $helperData;
}
protected function _prepareAttributeCollection($collection)
{
$collection->addIsFilterableFilter();
$collection->removePriceFilter();
return $collection;
}
}
Hide price cart items
- Vendor\Module\Model\Directory\PriceCurrency.php
==>> function format(
$amount,
$includeContainer = true,
$precision = self::DEFAULT_PRECISION,
$scope = null,
$currency = null
)
Hide price from product options, cart total, checkcout totals
- Vendor\Module\view\frontend\requirejs-config.js
==>> var config = {
'config': {
'mixins': {
'Magento_Catalog/js/price-utils': {
'Vendor_Module/js/price-utils-mixin': true
}
}
}
};
- Vendor\Module\view\frontend\web\js\price-utils-mixin.js
==>>
/**
* @api
*/
define([
'jquery',
'underscore'
], function ($, _) {
'use strict';
var globalPriceFormat = {
requiredPrecision: 2,
integerRequired: 1,
decimalSymbol: ',',
groupSymbol: ',',
groupLength: ','
};
return function (target) {
target.formatPrice = function formatPrice(amount, format, isShowSign) {
var s = '', precision, integerRequired, decimalSymbol, groupSymbol, groupLength, pattern, i, pad, j, re, r, am;
format = _.extend(globalPriceFormat, format);
// copied from price-option.js | Could be refactored with varien/js.js
precision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision;
integerRequired = isNaN(format.integerRequired = Math.abs(format.integerRequired)) ? 1 : format.integerRequired;
decimalSymbol = format.decimalSymbol === undefined ? ',' : format.decimalSymbol;
groupSymbol = format.groupSymbol === undefined ? '.' : format.groupSymbol;
groupLength = format.groupLength === undefined ? 3 : format.groupLength;
pattern = format.pattern || '%s';
if (isShowSign === undefined || isShowSign === true) {
s = amount groupLength ? i.length % groupLength : 0;
re = new RegExp('(\\d{' + groupLength + '})(?=\\d)', 'g');
// replace(/-/, 0) is only for fixing Safari bug which appears
// when Math.abs(0).toFixed() executed on '0' number.
// Result is '0.-0' :(
am = Number(Math.round(Math.abs(amount - i) + 'e+' + precision) + ('e-' + precision));
r = (j ? i.substr(0, j) + groupSymbol : '') +
i.substr(j).replace(re, '$1' + groupSymbol) +
(precision ? decimalSymbol + am.toFixed(precision).replace(/-/, 0).slice(2) : '');
//return pattern.replace('%s', r).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
return '';
};
return target;
};
/**
* Repeats {string} {times} times
* @param {String} string
* @param {Number} times
* @return {String}
*/
function stringPad(string, times) {
return (new Array(times + 1)).join(string);
}
});