Magento 2E-CommerceHow To GuideApril 24, 2024Simon Walker

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

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

While reading the title, you may be wondering why any store would want to hide product prices in Magento 2 regardless of whether the customer is logged in. After all, price is often the topmost or only factor the target audience considers when contemplating a purchase.

However, there are situations such as guest users where you may want to hide product prices. In this article, we will guide you about everything there is to know about the Magento 2 hide price for guest. Before doing that, let’s discuss why you need to implement the Magento 2 hide price if not logged in feature.

hide-price-for-guest

Hide Price For Guest

Hide Price For Guest extension allows you to hide prices from non-logged-in visitors.

Why You Need to Hide Price if Not Logged-In?

Magento 2 hide prices unless logged in feature is something that every store owner should consider. Here’s why:

1. Encouraging User Registration

You may have heard the phrase data is the new oil. Given that we are living in the digital age, data is indeed the new oil. A guest user does not offer much data to store owners which they can use to create personalised marketing strategies. When the same user creates an account, they share information such as their email, phone number, and more.

From a store owner’s perspective, this information is a gold mine. They can use it to send personalised messages, recommend products that align with the user’s interests, and more. If users don’t have any incentive to register with your store, 9 times out of 10, they will ignore the registration process. However, by hiding the prices, you are giving them a good reason to create an account.

2. Segmentation

Suppose your store gets 1000 users daily. 900 are guest users, meaning they are not logged in. How would you know how to retarget these visitors or what products to recommend them? When users are logged in, you can easily gather information related to their preferences, purchasing behaviour, and demographics. By segmenting users based on different parameters, you can create more effective marketing strategies and recommend the most relevant products. This can boost your sales and customer engagement.

3. Control over Pricing Visibility

In some cases, you need to implement the Magento 2 hide price for guest feature simply because of your pricing strategy. You may opt for tier pricing or sell on different rates to each customer. Therefore, there’s no point in displaying any price. Instead, you need to add a ‘Ask for Quote’ button.

Check Out Our Extensions Related to Tier Pricing:

4. Preventing Price Comparison

It wouldn’t be wrong to say that everyone is trying their hands at eCommerce nowadays. From the customer’s perspective, more stores mean more choice. From the store owners’ perspective, it means high competition. While competition is always healthy, too much of it can overwhelm businesses. Customers tend to browse 10, 15 sites before committing to a purchase.

If you wish to avoid becoming just another website that users flip through when considering a product, hide the prices. This will capture their curiosity and compel to sign up. This is where you can convert them to a customer. As soon as they sign up, start sending them personalised messages. With the use of engaging messages and promotions, you can compel users to stick with your website.

5. Differentiate Yourself

One of the ways to attain success in eCommerce is through differentiation. Everyone is displaying the prices. By hiding the prices, you can create a point of differentiation. Create curiosity in your audience’s minds and compel them to sign up.

How to ‘Magento 2 Hide Price if not Logged In’

There are different ways to hide the price in Magento 2 for guest users. Let’s discuss them in detail.

Method 1 - Magento 2 Hide Price for Guest Extension

One of the easiest methods to hide the prices for not logged in users is to use an extension. We already have one for this very purpose.

Of course, this extension offers a lot more than just hiding prices for not logged in users. Through this extension, you can hide prices for specific products and customer groups.

hiding-prices-by-fmeextensions

You can also hide prices for specific product categories.

product-categories

That’s not all. You can hide prices for specific time periods. This can be a great help during holiday seasons when eCommerce sales really take off.

add-start-and-end-date

If you are interested in knowing more about this extension, book a live demo.

Method 2 – Custom Code

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 add to cart button, Magento 2 hide price for guest is the extension you will want to use.

Conclusion

This concludes our article on how to add the price for not logged in customers in Magento 2. When used correctly, this feature can be a great way to boost your sales and customer engagement. Please feel free to contact us if you have any questions.

Related Articles: