1

I have use events and observers to check the whther the user is logged in or not. And the logout user will not see the price of the product. Here's my code:

Created events.xml file in app/code/Demo/Mymodule/etc/frontend:

<?xml version="1.0" encoding="UTF-8"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
        <event name="layout_load_before">
            <observer name="add_layout_handles" instance="Demo\Mymodule\Observer\AddHandles" />
        </event>
    </config>

Created Addhandles.php in app/code/Demo/Mymdoule/Observer:

<?php

namespace Demo\Mymodule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;

class AddHandles implements ObserverInterface
{
    /**
    * @var CustomerSession
    */
    protected $customerSession;
    /**
    * Add constructor.
    * @param CustomerSession $customerSession
    */
    public function __construct(CustomerSession $customerSession)
    {
        $this->customerSession = $customerSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $layout = $observer->getEvent()->getLayout();


        if (!$this->customerSession->isLoggedIn())
        {
            $layout->getUpdate()->addHandle('customer_log_out');
        }
    }
}

Created customer_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>

Created final_price.phtml in app/code/Demo/Mymodule/view/frontend/templates/product/price which have my custom message.

But it's not working. Can please someone help me.

  • why you don't override it like as app/design/frontend/Vendor/themename/Magento_Catalog/templates/product/price/final_price.phtml ? – Ansar Husain Jul 27 '18 at 11:12
0

Override class:

\Magento\Catalog\Pricing\Render\FinalPriceBox

And in that override method:

public function wrapResult($html)

and in that you can get product attribute like:

if($this->getSaleableItem()->getXyzIsHide()) { return ""; } else { ... }

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.