TransWikia.com

Hide Accound Dashboard Link depending upon customer group

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.

One Answer

I would suggest below reference code. There are two ways to do this

  1. Create a new layout that will work filter based on the customer group. Forex customer_group_general.xml. In this file, you can hide/remove the left navigation link which you have added using the XML file. Please follow the steps to create a new layout:
  • Create a new module
  • Add di.xml file in the etc/frontend folder
<?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>
  • Create plugin php file in your module in app/code/[codePool]/[Vendor]/[ModuleName]/Plugin/[ClassName].php
<?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.

https://www.smartiehastheanswer.co.uk/magento2/adding-custom-dynamic-layout-handles-globally-in-magento2.html

  1. Create a custom link that will come from the phtml file. In phtml file, you can add your conditions and it will update in the left navigation.
<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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP