Magento Asked by Anuraag on January 19, 2021
I have added two links to customer dashboard "Proposals" and "Documentaion". I have also added a customer group "xyz". I want to only show the mentioned group. Similarly, I want to hide these two links for other groups.
I would suggest below reference code. There are two ways to do this
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="MagentoFrameworkViewResultPage"> <plugin name="[pluginName]" type="[Vendor][ModuleName]Plugin[ClassName]" sortOrder="10"/> </type> </config>
<?php namespace [Vendor][ModuleName]Plugin; use MagentoCustomerModelSession; use MagentoFrameworkViewResultPage; use MagentoStoreModelGroupFactory; class [ClassName] { protected $customerSession; protected $groupFactory; public function __construct( Session $customerSession, GroupFactory $groupFactory ) { $this->customerSession = $customerSession; $this->groupFactory = $groupFactory; } public function afterAddPageLayoutHandles(Page $subject) { $group = $this->groupFactory->create() ->load($this->customerSession->getCustomerGroupId()); if ($group->getId() >= 0) { $handle = sprintf("customer_group_%s", strtolower(preg_replace("/[^A-Za-z0-9]/", '_', $group->getName()))); $subject->addHandle($handle); } return $subject; } }
Now you can create a customer_group_default.xml and update the link based on your requirement. Please review the reference link for more details.
<block class="CustomSalesBlockLink" name="downloads-new" template="Custom_Sales::link.phtml"> <arguments> <argument name="sortOrder" xsi:type="number">210</argument> </arguments> </block>
link.phtml
<?php if($block->hidelink()):?> <li class="custom link" > <a href="<?= /* @noEscape */ $block->getUrl('test/index/index')?>" class="custom-link"> <?php echo __("My Link")?> </a> </li> <?php }?>
Link.php
<?php namespace CustomSalesBlock; use MagentoCustomerBlockAccountSortLinkInterface; /** * Class Link * * @api * @SuppressWarnings(PHPMD.DepthOfInheritance) * @since 100.0.2 */ class Link extends MagentoFrameworkViewElementHtmlLink implements SortLinkInterface { /** * {@inheritdoc} * @since 101.0.0 */ public function getSortOrder() { return $this->getData(self::SORT_ORDER); } public function hideLink() { //Add condition return false; } }
In block, you can load current logged in customer and sent true or false flag in the link.phtml
I hope this will help you.
Thanks. If you need any help, please let me know.
Answered by Nits on January 19, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP