5

I don't want to use any commercial module for hiding price.and i don't want hide by using templates. is there any way to hide price from entire site using plug-ins or rewrites?

4
11

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 '';  

    }

}
5
  • its not working. changes not effect.
    – Yogesh
    Jul 6 '17 at 10:23
  • 1
    FinalBox isn't cached ?
    – siddhesh
    Jul 14 '17 at 15:20
  • Works great. I need to hide regular/final price on related section and how to achieve that?
    – Jarnail S
    Aug 18 '17 at 11:24
  • well, i just figure out that this only worked for homepage and product detail page, but not with WISH LIST page. I still have change phtml file
    – fudu
    Oct 4 '18 at 2:43
  • Can you please tell me how to get a product id within this plugin Feb 13 '20 at 5:46
5

Create module (eg: CatalogHidePrice) with plugin.
di.xml should look like:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Pricing\Render\FinalPriceBox">
        <plugin name="cataloghideprice_finalpricebox" type="NAMESPACE\CatalogHidePrice\Plugin\FinalPriceBox"/>
    </type>
</config>

In the plugin class \Plugin\FinalPriceBox\FinalPriceBox do an around plugin (for me, I have to hide the price based on a product attribute)

function aroundToHtml($subject, callable $proceed)
{

    if($subject->getSaleableItem()->getHidePrice()==1){
        return '';
    }else{
        return $proceed();
    }

}
2
  • I can't get the value of my attribute. These methods don't work: getMyAttribute() and getData('my_attribute'). Although the following method works $_product = $subject->getSaleableItem(); $myAttribute = $_product->getResource()->getAttribute('my_attribute')->getFrontend()->getValue($_product);. What could be the problem?
    – Victor S.
    Jul 5 '18 at 15:21
  • Lomosh HidePrice is an attribte?
    – Nagaraju K
    Nov 2 '18 at 6:56
0

The simplest way I can think of would be to just target the price's CSS class and hide it.

No modifying of any themes/template files required and can easily revert the changes by removing the style rule.

2
  • Have you got any example of this? Jul 12 '19 at 8:51
  • @jibingeorge it would vary depending on the theme/template, but just find the css class that is attached to the element you want hidden, then add or modify a style declaration to set it to display: none
    – codestr
    Aug 2 '19 at 2:19
0

I think this extension may solve your problems: https://www.mageplaza.com/magento-2-hide-price/

0

Please try our free plugin Magento 2 Hide Price Free. It hides prices for unauthorised Magento customers or replaces it with custom text or button with the popup message. You can configure it on store view level. Let us know how it worked for you!

https://mageme.com/magento-2-hide-price-extension-free.html

0

Try This code

app/code/Vendorename/ModuleName/etc/frontend

di.xml

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Pricing\Render\FinalPriceBox">
        <plugin name="price_hide" type="Vendorename\ModuleName\Plugin\HidePriceBox" sortOrder="1" disabled="false"/>
    </type>
</config>

app/code/Vendorename/ModuleName/Plugin

HidePriceBox.php

<?php

namespace Vendorename\ModuleName\Plugin;

class HidePriceBox
{

    function afterToHtml(\Magento\Catalog\Pricing\Render\FinalPriceBox $subject, $result)
    {
        // add your logic
        if (condition) {
            return '';
        } else {
            return $result;
        }
    }
}

I Hope This Helps You.

0

This isnt working!

Magento\Catalog\Pricing\Render\FinalPriceBox has wrapResult and _toHtml both are protected methods and you cannot write the plugin on protected method.

Only preference could work by overriding either wrapResult or _toHtml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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