3

enter image description here

app\code\companyname\modulename\view\frontend\layout\default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="Codility_Customers::css/firstfile.css" media="all and (min-width: 1px)"/>
    </head>
    <body>
    <referenceBlock name="customer_account_navigation">
        <block class="Magento\Customer\Block\Account\SortLinkInterface" name="seller-dashboard-link">
            <arguments>
                <argument name="path" xsi:type="string">seller/customer/index</argument>
                <argument name="label" xsi:type="string">Link 1</argument>
                <argument name="sortOrder" xsi:type="number">800</argument>
            </arguments>
        </block>
        <block class="Magento\Customer\Block\Account\SortLinkInterface" name="seller-dashboard-myorder">
            <arguments>
                <argument name="path" xsi:type="string">seller/customer/orders</argument>
                <argument name="label" xsi:type="string">Link 2</argument>
                <argument name="sortOrder" xsi:type="number">700</argument>
            </arguments>
        </block>

   </referenceBlock>
    </body>
</page>

Now I want to hid Link 1 or Link-2 on some condition. e.g I will check on specific customer group

if(customergroup=="General"){
// then show Tabs otherwise Tabs will not show
}

anyone guide me how put condition here?

3 Answers 3

1

You can create custom module or existing your any custom module will add this files.

Create a custom file

Vendor\Module_name\Model\Config\Source\Sections

    <?php
            namespace Vendor\Module_name\Model\Config\Source;

            use Magento\Framework\App\Utility\Files;
            use Magento\Framework\Option\ArrayInterface;
            use Magento\Framework\View\Element\Html\Links;

            class Sections implements ArrayInterface
            {
                /** @var Files  */
                protected $utilityFiles;

                protected $links;

                protected $list = [];

                public function __construct(
                    Files $utilityFiles,
                    Links $links
                ) {
                    $this->utilityFiles = $utilityFiles;
                    $this->links = $links;
                }
                /**
                 * @return array
                 */
                public function toOptionArray()
                {
                    return $this->getSections();
                }

                /**
                 * @return array
                 */
                public function getSections()
                {
                    $fileList = $this->utilityFiles->getLayoutFiles(['area_name' => 'frontend'], false);

                    foreach ($fileList as $configFile) {
                        if (strpos($configFile, 'customer_account.xml') !== false) {
                            $configXml = simplexml_load_file($configFile);
                            $this->processXmlElement($configXml);
                        }
                    }

                    return $this->list;
                }

                /**
                 * @param $configXml
                 */
                protected function processXmlElement($configXml)
                {
                    if ($referenceBlocks = $configXml->body->referenceBlock) {
                        foreach ($referenceBlocks as $referenceBlock) {
                            if (!empty($referenceBlock->xpath('block/arguments/argument[@name="label"]'))) {
                                $this->updateReferenceBlockList($referenceBlock);
                            }
                        }
                    }

                    if (isset($configXml->body->referenceContainer) && isset($configXml->body->referenceContainer->block)) {
                        if (isset($configXml->body->referenceContainer->block->block)
                            && isset($configXml->body->referenceContainer->block->block->block)
                        ) {
                            $referenceContainerBlocks = $configXml->body->referenceContainer->block->block->block;

                            for ($count = 0; $count < count($referenceContainerBlocks); $count++) {
                                if (!empty($referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]'))) {
                                    $this->updateReferenceContainerList($referenceContainerBlocks, $count);
                                }
                            }
                        } elseif (isset($configXml->body->referenceContainer->block->block)) {
                            $referenceContainerBlocks = $configXml->body->referenceContainer->block->block;

                            for ($count = 0; $count < count($referenceContainerBlocks); $count++) {
                                if (!empty($referenceContainerBlocks[$count]->xpath('arguments/argument[@name="label"]'))) {
                                    $this->updateReferenceContainerList($referenceContainerBlocks, $count);
                                }
                            }
                        }
                    }
                }

                /**
                 * @param $rcb
                 * @param $count
                 */
                protected function updateReferenceContainerList($rcb, $count)
                {
                    $this->list[(string) $rcb[$count]['name']] = [
                        'value' => (string) $rcb[$count]['name'],
                        'label' => (string) $rcb[$count]->xpath('arguments/argument[@name="label"]')[0],
                        'path' => (string) $rcb[$count]->xpath('arguments/argument[@name="path"]')[0]
                    ];
                }

                /**
                 * @param $rb
                 */
                protected function updateReferenceBlockList($rb)
                {
                    $this->list[(string) $rb->block['name']] = [
                        'value' => (string) $rb->block['name'],
                        'label' => (string) $rb->xpath('block/arguments/argument[@name="label"]')[0],
                        'path' => (string) $rb->xpath('block/arguments/argument[@name="path"]')[0]

                    ];
                }
            }

overide this file in your custom theme

vendor\magento\module-theme\view\frontend\templates\html\collapsible.phtml

  <?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerLinks = $objectManager->get("Vendor\Module_name\Model\Config\Source\Sections")->toOptionArray();

                    //echo "<pre>";print_r($customerLinks);
                    $customerSession = $objectManager->create("Magento\Customer\Model\Session");

                    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

                    $baseUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

                    ?>
                    <div class="block <?= /* @escapeNotVerified */ $block->getBlockCss() ?>">
                        <div class="title <?= /* @escapeNotVerified */ $block->getBlockCss() ?>-title" data-mage-init='{"toggleAdvanced": {"toggleContainers": "#<?= /* @escapeNotVerified */ $block->getBlockCss() ?>", "selectorsToggleClass": "active"}}'>
                            <strong><?= /* @escapeNotVerified */ __($block->getBlockTitle()) ?></strong>
                        </div>
                        <div class="content <?= /* @escapeNotVerified */ $block->getBlockCss() ?>-content" id="<?= /* @escapeNotVerified */ $block->getBlockCss() ?>">
                        <?php if($block->getBlockCss() == 'block-collapsible-nav' && $customerSession->isLoggedIn()){
                                ?>
                                <ul class="nav items">
                                <?php foreach ($customerLinks as $links) { 
                                    if($customerSession->getCustomerGroupId() == 1 && $links['label'] == 'My Product Reviews'){
                                        //dont show for these customer product reviews
                                        continue;
                                    }
                                    $url =  $baseUrl.$links['path']; ?>
                                    <li class="nav item"><a href="<?php echo $url; ?>"><?php echo $links['label']; ?></a></li>
                                <?php } ?>
                                </ul>
                        <?php   }
                        else { ?>
                            <?= $block->getChildHtml() ?>
                        <?php } ?>
                        </div>
                    </div>

Try this as per your customised it.

7
  • little work but very lengthy code . i try it
    – HaFiz Umer
    Jun 26, 2019 at 10:37
  • @HaFizUmer hahahaha yes bro..but cool Jun 26, 2019 at 10:39
  • i don't add tab from customer_account.xml . in use referenceBlock in default.xml
    – HaFiz Umer
    Jun 26, 2019 at 10:43
  • Brother you must add in customer_account.xml Jun 26, 2019 at 10:50
  • Okay Brother let me try wait please :)
    – HaFiz Umer
    Jun 26, 2019 at 10:51
1

Create two separate block classes for each link. In those classes override _toHtml() method and check for customer's group. If the customer belongs to desired group, then return parent::_toHtml(); else return '';

1
  • can you explain more ? if you update your answer ?
    – HaFiz Umer
    Jun 26, 2019 at 10:08
0
  1. Create a new class: VendorName\Customer\Block\Account\LinkUnderCondition.php

    namespace VendorName\Customer\Block\Account;
    
    use Magento\Customer\Block\Account\SortLink;
    use Magento\Framework\App\DefaultPathInterface;
    use Magento\Framework\View\Element\Template\Context;
    
    class LinkUnderCondition extends SortLink
    {
        public function __construct(
            Context $context,
            DefaultPathInterface $defaultPath,
            array $data = [],
        ) {
            // you can add here more dependencies if needed
            parent::__construct($context, $defaultPath, $data);
        }
    
        public function _toHtml()
        {
            if(true) { // change to your needed condition
                return parent::_toHtml();
            }
        }
    }
    

  1. Change the block classes for the custom links accordingly in your layout xml files. Example result:
<block class="VendorName\Customer\Block\Account\LinkUnderCondition" name="seller-dashboard-link">
            <arguments>
                <argument name="path" xsi:type="string">seller/customer/index</argument>
                <argument name="label" xsi:type="string">Link 1</argument>
                <argument name="sortOrder" xsi:type="number">800</argument>
            </arguments>
</block>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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