How to Hide Price for Not Logged In Customers in Magento 2

How to Hide Price for Not Logged In Customers in Magento 2

Hello Magento Folks,

In this article, I will illustrate How to Hide Price for Not Logged In Customers in Magento 2? Also, check our previously published article where I have illustrated How to Manage Order Status and Order state in Magento 2. Let’s Dig In.

Introduction: 

Mainly the product price is one of the most mandatory things that customers are eager to know. Many times the store merchants think to hide the product price for some reasons. The reasons include the tough competition in the market so the store merchant is not willing to show the price. Or the reason may also include that the store merchant wants the customer to register and login to view the price in their Magento 2 store. Ther store merchants will allow the registered and logged in customers to view price. Here the store merchants will keep the catalog accessible for all the customers but it will be displayed to the logged-in customers only.

This also helps the store merchants to increase Magento 2 registrations just the store merchants require to carry out some strategic game plan for converting the visitors to customers. For this strategic game plan, one point involves hide the price of products for guests or not logged in users! When the price is hidden then the customers can’t judge the product and they will be interested in viewing the price. Viewing the product price is necessary as this will boost the customers buying point of view. 

Follow the below steps to hide price for not logged in customers in Magento 2.

Are you on the hunt for advanced features for hiding price then checkout Magento 2 Hide Product Price Extension where you can find features like hiding the add to cart button for not logged in customer also you can select your desired/specific customer groups? 

Steps to hide price for not logged in customers in Magento 2:

Step 1: First you need to add di.xml in the following path: Vendor\Extension\etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" 
        type="Vendor\Extension\Pricing\Render\FinalPriceBox" />
    <preference for="Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox" 
        type="Vendor\Extension\Pricing\Render\FinalPriceBox" />
</config>

Step 2: After this, you will require to create FinalPriceBox.php in the following path: Vendor\Extension\Pricing\Render

<?php

namespace Vendor\Extension\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 $httpContext;
    
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Pricing\SaleableInterface $saleableItem,
        \Magento\Framework\Pricing\Price\PriceInterface $price,
        \Magento\Framework\Pricing\Render\RendererPool $rendererPool,
        array $data = [],
        \Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface $salableResolver = null,
        \Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface $minimalPriceCalculator = null,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
    	$this->httpContext = $httpContext;
        parent::__construct($context, 
                            $saleableItem, 
                            $price, 
                            $rendererPool, 
                            $data, 
                            $salableResolver, 
                            $minimalPriceCalculator);
    }
    
    protected function wrapResult($html)
    {
        $isLoggedIn =    $this->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>';
        }else{
            $wording = 'Please Login To See Price';
            return '<div class="" ' .
                'data-role="priceBox" ' .
                'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
                '>'.$wording.'</div>';
        }
    }
}

That’s It.

Conclusion:

Hence, implement the above code to hide price for not logged in customers in Magento 2. In case if you come across any difficulties in implementing the hide price for not logged in customers in Magento 2 feature then let me know in the comment section below. 

Also, spread the solution with your Magento friends via Social Media.

Happy Reading!

Previous Article

How to Configure Security Scan in Magento 2

Next Article

Why Video and Audio are Very Powerful Marketing Tools

Write a Comment
  1. The di.xml is not working. It says an error about the tags.

    ERROR:
    Couldn’t find end of Start Tag preference line 64
    Line: 65

    attributes construct error
    Line: 66

    Also the new error in the Final price box if we comment the preferences tag from di.xml.

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨