Magento 2: How to Hide Price for Not Logged in Customers?

Magento 2: How to Hide Price for Not Logged in Customers?
It’s very important to vividly display product prices because price is the one thing customers are most interested in. They want to compare prices before making their purchase decision. However, there are situations where you may want to hide product prices. For example, you may want to hide product price for not logged in or guest users.

Why You Need to Hide Price If Not Logged-in?

This can be your strategy to increase store registrations. If a guest user really wants to know the price, he can create an account and log in to see the price. Increased customer registrations provide you data to be used for retargeting and in email marketing. Hiding price for guests also prevents your prices from being exposed to competitors.

How to Hide Price Until Login in Magento 2?

Following is the code you need to add to hide price for not logged in users in Magento 2.

Step 1. Create di.xml in Vendor\Extension\etc folder.
<?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. Create FinalPriceBox.php at Vendor\Extension\Pricing\Render folder.
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
{
 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 = 'Login To See Price';
 return '<div class="" ' .'data-role="priceBox" ' .'data-product-id="' . $this->getSaleableItem()->getId() . '"' . '>'.$wording.'</div>';
 }
 }
}
The above code will hide price for non logged in customers in Magento 2. If you want to implement advanced features such as hiding price and add to cart button for specific customer groups, on upsell and cross sell, on search result pages and replacing add to cart with custom text or link, Magento 2 hide price for guest is the extension you may want to use. hide-price-for-guest-extension-for-magento-2

Please feel free to contact us if you have any questions.

Related Articles: