How To

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!

Click to rate this post!
[Total: 12 Average: 3.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • 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.

    • Confirm you have created a basic required files for the extension and your extension is enabled

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago