Question

How to hide price for guest user with plugin functionality in product pages and category pages?

Was it helpful?

Solution

app/code/Ravindra/Hideprice/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Ravindra_Hideprice" setup_version="2.2.0">

    </module>
</config>

app/code/Ravindra/Hideprice/registration.php

app/code/Ravindra/Hideprice/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Ravindra\Hideprice\Pricing\Render\FinalPriceBox" />
    <preference for="Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox" type="Ravindra\Hideprice\Pricing\Render\FinalPriceBox" />
</config>

app/code/Ravindra/Hideprice/Pricing/Render/FinalPriceBox.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Ravindra\Hideprice\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 for final_price rendering
 *
 * @method bool getUseLinkForAsLowAs()
 * @method bool getDisplayMinimalPrice()
 */
class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox
{

    /**
     * Wrap with standard required container
     *
     * @param string $html
     * @return string
     */
    protected function wrapResult($html)
    {

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();           
        $httpContext=$objectManager->get('Magento\Framework\App\Http\Context');
        $isLoggedIn=$httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

        if($isLoggedIn){

            return '<div class="price-box ' . $this->getData('css_classes') . '" ' .
                'data-role="priceBox" ' .
                'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
                '>' . $html . '</div>'; 

        }
    }
}

app/code/Ravindra/Hideprice/Helper/Data.php

<?php
namespace Ravindra\Hideprice\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper 
{
        public function __construct(
        \Magento\Framework\App\Helper\Context $context,

        array $data = []
        ) {

        parent::__construct($context);
        }
        /**
         * Functionality to get configuration values of plugin
         *
         * @param $configPath: System xml config path
         * @return value of requested configuration
         */

        public function getConfig($configPath) {
            return $this->scopeConfig->getValue(
                $configPath, \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            );
        }
}

app/code/Ravindra/Hideprice/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="hideprice ravindra" translate="label" sortOrder="1000">
            <label>Ravidevs</label>
        </tab>
        <section id="hideprice" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Hide Price</label>
            <tab>hideprice</tab>
            <resource>Ravindra_Hideprice::Ravindra_Hideprice</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <!-- Enable or disable price -->                
                <label>Hide Price General Setting</label>
                <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable/Disable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="hide_price_for_guest_user" translate="label" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Hide Price For Guest User</label>                    
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="user_login_text" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">           
                    <label>User Login Text</label>
                     <comment><![CDATA[
                       add class "withReferralUrl" <br> Ex. &lt;a href="#" class="withReferralUrl"&gt;login&lt;/a&gt;
                    ]]></comment>                                       
                    <depends>
                        <field id="hide_price_for_guest_user">1</field>
                    </depends>
                </field>
                <field id="where_display_message" translate="label" type="multiselect" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Display Login Text On</label>
                    <depends>
                        <field id="hide_price_for_guest_user">1</field>
                    </depends>
                    <source_model>Ravindra\Hideprice\Model\Config\Source\DisplayMessageListPage</source_model>
                </field>
                <field id="login_text_css" translate="label" type="textarea" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1">            
                    <label>Login Text Css</label>               
                    <depends>
                        <field id="hide_price_for_guest_user">1</field>
                    </depends>
                </field>    
            </group>
        </section>
    </system>
</config>

app/code/Ravindra/Hideprice/Model/Config/Source/DisplayMessageListPage.php

<?php
namespace Ravindra\Hideprice\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class DisplayMessageListPage implements ArrayInterface
{
   public function toOptionArray()
    {
        return [
            ['value' => 'none', 'label' => __('None')],
            ['value' => 'catalog_category_view', 'label' => __('Catalog Category View')],
            ['value' => 'catalog_product_view', 'label' => __('Catalog Product View')],
            ['value' => 'cms_index_index', 'label' => __('Cms Page')],

        ];
    }

}

OTHER TIPS

Hide price for guest user

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
{
    protected $_customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $session
    ) {
        $this->_customerSession = $session;
    }

    function afterToHtml(\Magento\Catalog\Pricing\Render\FinalPriceBox $subject, $result)
    {
        if ($this->_customerSession->isLoggedIn()) {
            return $result;
        } else {
            return '';
        }
    }
}

I Hope This Helps You.

Extension ProductAvailable allows the store owner to hide the product price and add to cart button from guests or certain customer groups.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top