3

How to hide the price for guest user only wholesale group user can see price in is there any script or code or some free extension please suggest ?

3 Answers 3

2

Product Available Extension allows the store owner to hide the product price and add to cart button from guests or certain customer groups.

1

You can hide the price by override the wrapResult function in vendor/magento/module-catalog/Pricing/Render/FinalPriceBox.php

app/code/Vendor/Module/etc/di.xml

<preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Vendor\Module\Pricing\Render\FinalPriceBox" />

app/code/Vendor/Module/Pricing/Render/FinalPriceBox.php

<?php

namespace Vendor\Module\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 function wrapResult($html)
    {
     $result = '';
     $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){
       $result = '<div class="price-box ' . $this->getData('css_classes') . '" ' .
        'data-role="priceBox" ' .
        'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
        '>' . $html . '</div>';
     }
    return $result; 
    }

}
7
  • Hello Vinz , i have tried your code it is not working
    – Sheenu
    Commented Jan 18, 2018 at 11:16
  • On which page do you need to hide the price ?
    – Vinz
    Commented Jan 18, 2018 at 13:43
  • i want to hide product list page, product details page and everywhere for guest user only login user with wholesale group can see price
    – Sheenu
    Commented Jan 19, 2018 at 10:34
  • @Vinz Does this solution also work with FPC? Won't it save the $isLoggedIn state of the first user that saves the FPC? Commented Jan 3, 2019 at 11:08
  • Hi,@Vinz,I tried above script but still no effect on price.can you provide some other method? Commented Feb 20, 2020 at 10:22
1

Adding my opinion to @Vinz answer, you should get logged-in customer group id

<?php $context = $objectManager->get('Magento\Framework\App\Http\Context'); ?>
<?php $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); ?>
<?php $customerGroupId = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_GROUP); ?>

<?php if($isLoggedIn && $customerGroupID == 'WHOLESALE ENTITY ID') // you should put the entity id to WHOLESALE ENTITY ID

   <?php $result = '<div class="price-box ' . $this->getData('css_classes') . '" ' .
    'data-role="priceBox" ' .
    'data-product-id="' . $this->getSaleableItem()->getId() . '"' .
    '>' . $html . '</div>';
 ?>
return $result; 
1
  • It is not working
    – Sheenu
    Commented Jan 18, 2018 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.