0

I have created one module and it has system configuration on the admin side, where the admin has the functionality to enable/disable the module. As per system configuration, I need to hide the price from every place on the front end.

i.e. "catalog, category, wishlist, a related product, upsell products, cart, checkout, orders, email, account dashboard, etc.."

from everywhere I need to hide the price if the module is enabled.

Is there any global file for the price which can be overridden and add the code for hiding price?

I found one file, But if we override \Magento\Catalog\Pricing\Render\FinalPriceBox and change in wrapResult() function, it works but it will affect only product page and home page not for wishlist and other places.

how can I achieve this functionality? Is anyone have any idea about it?

Please share an idea to hide price from cart/checkout/order/etc...

2 Answers 2

1

Try below code:

app/code/Vendor/Module/etc/di.xml

<preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Vendor\Module\Pricing\Render\FinalPriceBox" />

app/code/Vendor/Module/Pricing/Render/FinalPriceBox.php

<?php

namespace Vendor\Module\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)
    {
        return '';
    }

}
2
  • i already used above code but it only hide price from category, product view. not other pages. mentioned in question. Dec 20, 2018 at 12:47
  • Partially accepted. :) Dec 20, 2018 at 12:50
1

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);
    }   
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.