Magento has an out of box feature to hide price in Adobe Commerce version (Enterprise Edition). Navigate to Catalog > Categories. Open the desired category. Scroll down to reveal the “Category Permissions” tab like below screenshot.
Create permission rule following below steps:
Step1: Click on the New Permissions Button, permissions option fields look like below screenshot.
Step2: Select the Customer Group, Browsing Category, Display Product Prices and Add To cart according to the requirement.
Step3: Last step, save the configuration and flush full-page cache.
The price will hide and denied add to cart for guest user as per set the rule.
Community Edition:
We need to do some customization on code level to achieve this task in community edition.
Step1: create store configuration to configure category id for those need to hide price.
1 2 3 4 5 6 7 8 9 |
<group id="bluethink_catalog_configurations" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Categories Configurations</label> <field type="text" id=" hide_price " translate="label" sortOrder="12" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Hide Prices for Categories</label> <comment>Add comma separated category ids</comment> </field> </group> |
Step2: Override the function: Magento\Catalog\Pricing\Render\FinalPriceBox:: wrapResult($html)
Step2.1: create Bluethink/Catalog/etc/di.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">< preference for="Magento\Catalog\Pricing\Render\FinalPriceBox" type="Bluethink\Catalog\Pricing\Render\FinalPriceBox" /> <preference for="Magento\ConfigurableProduct\Pricing\Render\FinalPriceBox" type=" Bluethink \Catalog\Pricing\Render\FinalPriceBox" /> </config> |
Step2.2: create file Bluethink\Catalog\Pricing\Render\FinalPriceBox.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
<?php namespace Bluethink\Catalog\Pricing\Render; use Magento\Catalog\Model\CategoryFactory; use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface; use Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface; use Magento\Catalog\Pricing\Render\FinalPriceBox as RenderFinalPriceBox; use Magento\Customer\Model\Context as modelContext; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\Pricing\Price\PriceInterface; use Magento\Framework\Pricing\Render\RendererPool; use Magento\Framework\Pricing\SaleableInterface; use Magento\Framework\View\Element\Template\Context; use Magento\Store\Model\ScopeInterface; class FinalPriceBox extends RenderFinalPriceBox { /** * @var SaleableInterface */ protected $saleableItem; /** * @var PriceInterface */ protected $price; /** * @var RendererPool */ protected $rendererPool; /** * @var HttpContext */ protected $httpContext; /** * @var CategoryFactory */ protected $categoryFactory; public function __construct( Context $context, SaleableInterface $saleableItem, PriceInterface $price, RendererPool $rendererPool, HttpContext $httpContext, CategoryFactory $categoryFactory, ScopeConfigInterface $scopeConfigInterface, array $data = [], SalableResolverInterface $salableResolver = null, MinimalPriceCalculatorInterface $minimalPriceCalculator = null ) { parent::__construct($context, $saleableItem, $price, $rendererPool, $data, $salableResolver, $minimalPriceCalculator); $this->categoryFactory = $categoryFactory; $this->httpContext = $httpContext; $this->scopeConfigInterface = $scopeConfigInterface; } protected function wrapResult($html) { $categoryPath = $this->getCategoryPath($this->getSaleableItem()->getCategoryId()); $categoryToHidePrice = $this->getCategoryToHidePrice(); $isLoggedIn = $this->httpContext->getValue(modelContext::CONTEXT_AUTH); $flag = false; foreach ($categoryToHidePrice as $category) { if (in_array($category, $categoryPath)) { $flag = true; } } if (!$isLoggedIn && $flag) { return '<div class="price-box price-final_price" ' . 'data-role="priceBox" ' . 'data-product-id="' . $this->getSaleableItem()->getId() . '"' . '></div>'; } else { return '<div class="price-box ' . $this->getData('css_classes') . '" ' . 'data-role="priceBox" ' . 'data-product-id="' . $this->getSaleableItem()->getId() . '" ' . 'data-price-box="product-id-' . $this->getSaleableItem()->getId() . '"' . '>' . $html . '</div>'; } } /** * @param $categoryId * @return array|string[] */ public function getCategoryPath($categoryId) { $category = $this->categoryFactory->create()->load($categoryId); if ($category->getId()) { $categoryPath = $category->getPath(); $categoryPath = explode('/', $categoryPath); return $categoryPath; } else { return []; } } /** * @return string[] */ public function getCategoryToHidePrice() { $categories = $this->scopeConfigInterface->getValue('bluethink_catalog/bluethink_catalog_configurations/hide_price', ScopeInterface::SCOPE_STORE); return explode(',', $categories?? ''); } } |
Step3: Run the below commands
sudo php bin/magento s:d:c
sudo php bin/magento c:f
Vikas Mishra
2024-01-10