1

I have created events observer which is working fine to check whether the customer is logged in or not. If the customer is not logged in then the price will not be displayed.

Here's my code:

custom_log_out.xml in

app/code/Demo/Mymodule/view/frontend/layout:

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
    <referenceBlock name="render.product.prices">
        <arguments>
            <argument name="default" xsi:type="array">
                <item name="prices" xsi:type="array">
                    <item name="final_price" xsi:type="array">
                        <item name="render_template" xsi:type="string">Demo_Mymodule::product/price/final_price.phtml</item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</layout>

Then created final_price.phtml in

app/code/Demo/Mymodule/view/frontend/templates/product/price

which have my custom code:

<p>Login to Continue</p>

But it's not working can please anyone guide me?

1 Answer 1

0

The best approach is to create plugin and use "around" on toHtml function in this class Magento\Catalog\Pricing\Render\FinalPriceBox

Create di.xml file in your module

<?xml version="1.0"?>
<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="{vendor}\{module}\Plugin\FinalPriceBox"/>
    </type>

</config>

Create this file {vendor}{module}\Plugin\FinalPriceBox.php

<?php

namespace {vendor}\{module}\Plugin;


class FinalPriceBox
{
    function aroundToHtml($subject, callable $proceed)
    {
        if($subject->getSaleableItem()->getHidePrice()==1){
            return '';
        }else{
            return $proceed();
        }

    }

}

Don't forget to replace {vendor} and {module} with your names.

after that execute those commands

php bin/magento setup:upgrade

php bin/magento setup:di:compile

2
  • Thanks for the response. Well, actually I am looking to do that using event observer. Commented Jul 20, 2018 at 13:41
  • Yes that may work, I tried the event observer but i had some issues, plugin help me a lot Commented Jul 20, 2018 at 13:43

Your Answer

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

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