0

I am using Magento 2.2.6 I need to customize and if any plugin available for logged-in customer show only price not show for guest user in Magento website. if anyone idea reply me. I have try to one module integrate but not hide price in listing and detail.

0

You need to override price renderer.

First, create a sample module. We consider here "Stackoverflow_Answer"

Create etc/di.xml and put below code

<preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Stackoverflow\Answer\Pricing\Render\FinalPriceBox" />
<preference for="Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox" type="Stackoverflow\Answer\Pricing\Render\FinalPriceBox" />

Now created FinalPriceBox.php under Pricing/Render folder and put below code.

<?php
namespace Stackoverflow\Answer\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>';
        }
    }
}

I had the same task yesterday, it works. Let me know if you face any issue.

  • Not working i m tried same code but not reflect on listing page and detail page – Rv Singh Apr 5 at 13:15
  • It should be. please check this class "Magento\Catalog\Pricing\Render\FinalPriceBox" and check wrapResult is called or not. It may be conflict with another module too. – Amitkumar solanki Apr 6 at 5:38

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.