2

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 ?

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; 
    }

}
  • Hello Vinz , i have tried your code it is not working – Sheenu Jan 18 '18 at 11:16
  • On which page do you need to hide the price ? – Vinz Jan 18 '18 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 Jan 19 '18 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? – Maarten Wolfsen Jan 3 at 11:08
1

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

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; 
  • It is not working – Sheenu Jan 18 '18 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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