6

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?

7

4 Answers 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
    Commented Jul 6, 2017 at 10:23
  • 1
    FinalBox isn't cached ?
    – siddhesh
    Commented Jul 14, 2017 at 15:20
  • Works great. I need to hide regular/final price on related section and how to achieve that?
    – Jarnail S
    Commented Aug 18, 2017 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
    Commented Oct 4, 2018 at 2:43
  • Can you please tell me how to get a product id within this plugin Commented Feb 13, 2020 at 5:46
8

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.
    Commented Jul 5, 2018 at 15:21
  • Lomosh HidePrice is an attribte? Commented Nov 2, 2018 at 6:56
2

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

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? Commented Jul 12, 2019 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
    Commented Aug 2, 2019 at 2:19

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