How to Hide Price for Not Logged In Customers in Magento 2
Product prices are something that users really want to know. Sometimes the store owner is not willing to show prices because of the competition and market or he wants the users to register and login to see the prices in Magento 2. He wants to keep the catalog available to all the users, but display prices only to registered customers. This is helpful when a Magento 2 store owner wants to keep pricing private.
To increase the Magento 2 registrations, one needs to implement techniques strategically that prompts visitors to register. One such technique is to hide the price of products for guests or not logged in users! The necessity to view price before making purchase decision encourage the guest users to register.
To hide price for not logged in customers in Magento 2, one needs to implement the below code.
Method to hide price for not logged in customers in Magento 2:
- Create di.xml in Vendor\Extension\etc folder
1234567<?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> - Create FinalPriceBox.php at Vendor\Extension\Pricing\Render folder
123456789101112131415161718192021222324252627282930313233343536373839404142434445namespace 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{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) {parent::__construct($context,$saleableItem,$price,$rendererPool,$data,$salableResolver,$minimalPriceCalculator);}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>';}else{$wording = 'Please Login To See Price';return '<div class="" ' .'data-role="priceBox" ' .'data-product-id="' . $this->getSaleableItem()->getId() . '"' .'>'.$wording.'</div>';}}}
The basic feature to hide price for not logged in customers in Magento 2 can be implemented with the above code but if you want advanced features such as hide price and “add to cart” button based on customer groups, redirect users to Contact Us page, set custom text to replace prices, etc. take a look at our Magento 2 Hide Price extension.
Please feel free to post any doubts in the comments section for me to solve it for you 🙂
Rate the post with 5 stars if found useful!
Thank You!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
12 Comments
It is working on product detail page and listing page, But when added to cart, its showing in cartpage, checkout page etc..
Hey Anas,
Please refer our given extension for such functionality:
https://meetanshi.com/magento-2-hide-price.html
Your comment is awaiting moderation.
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.
Hello Areeb,
There might be an issue in copying di.xml file from your end.
Thank You.
I have used this code but sometimes it worked sometimes not. After login on some products it still showing login to see price and for some products, price is displaying.
After removing the cache it works but again after refreshing the page again and again the price again disappeared for some products.
Its something wrong with cache?
Hello,
Are you using any caching tool?
It may be possible while using varnish cache.
Thank You.
hi is there any idea, whether we can stop entire price rendering to frontend instead of just hidinh throgh CSS, bec in my case google crawling the price and indexing . while searching in google its showing price. i want to hide in google search as well(hiding price in google search).
Hello,
You can remove the price block.
Thanks.
Hello, thanks for this, is there a way to show out of stock instead of hiding the button
Hello Roger,
In the file “FinalPriceBox.php” change the static content “Please Login to See Price” to “Out of Stock”
That’s it.
Thank you.
Any idea why this would not work for me? I followed your instructions exactly. The only odd thing, is I did not have an Extension folder inside my Vendor folder, so I created it. Using Magento 2.3.4. Any ideas??
Hello,
You need to implement the solution by developing a custom extension.
Also, in the place of “vendor”, you’ll have to use your company name or any custom name and in the place of “extension,” you need to use the name of the extension.
Please note that it is compulsory to follow these naming conventions.
Thanks.