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
<?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
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 = '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.