Condition on Custom Tab in Customer Account page
Question
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?
Solution
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.
OTHER TIPS
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 '';