How can I remove a specific navigation link (e.g., ‘Hot Sale’) for guest users and for logged-in users who are not in a specific customer group, by overriding the Swissup NavigationPro template in Magento 2?

Step 1: Create the Override Directory

Swissup’s menu.phtml is located at:

vendor/swissup/module-navigationpro/view/frontend/templates/menu.phtml

To override it in your custom theme (JJ), create the required directory:

mkdir -p szco/app/design/frontend/JJ/Swissup_Navigationpro/templates/

Step 2: Copy the menu.phtml File

Copy the original menu.phtml from the module to your theme:

cp szco/vendor/swissup/module-navigationpro/view/frontend/templates/menu.phtml szco/app/design/frontend/JJ/Swissup_Navigationpro/templates/menu.phtml

Step 3: Modify menu.phtml to Remove “Hot Sale” for Certain Users

Open the copied file and add the following logic to remove the “Hot Sale” link:

<?php
    /** @var SwissupNavigationproBlockMenu $block */
    $_menu = $block->getHtml('level-top');
    $_additionalHtml = $block->getChildHtml();
    $_isLazyInit = !!$block->getLazyInit();


    if (empty($_menu) && empty($_additionalHtml)) {
        return;
    }


    // Get customer session
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $customerSession = $objectManager->get(MagentoCustomerModelSession::class);
    $isLoggedIn = $customerSession->isLoggedIn();
    $customerGroupId = $isLoggedIn ? $customerSession->getCustomerGroupId() : null;


    // If not logged in or not in group 4, remove "Hot Sale"
    if (!$isLoggedIn || $customerGroupId != 4) {
        // Use DOMDocument to safely remove only "Hot Sale" links
        $dom = new DOMDocument();
        libxml_use_internal_errors(true); // Prevent errors from malformed HTML
        $dom->loadHTML('<div>' . $_menu . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        libxml_clear_errors();


        // Find all <a> elements
        foreach ($dom->getElementsByTagName('a') as $anchor) {
            if (stripos($anchor->nodeValue, 'Hot Sale') !== false) {
                $anchor->parentNode->removeChild($anchor); // Remove the "Hot Sale" link
            }
        }


        // Get the updated menu HTML
        $_menu = $dom->saveHTML();
    }
?>


<nav class="swissup-navigationpro navpro <?php echo $block->escapeHtml($block->getNavCssClass()) ?>"
    <?php /* @escapeNotVerified */ echo $block->getExtraAttributes() ?>>


    <ul id="<?= $block->getJsId() ?>"
        class="navpro-menu <?php echo $block->escapeHtml($block->getCssClass()) ?>"
        data-mage-init<?= $_isLazyInit ? '-lazy' : '' ?>='<?= $this->getJsLayout() ?>'
        >
        <?php /* Render the modified menu */ echo $_menu; ?>
        <?php /* @escapeNotVerified */ echo $_additionalHtml; ?>
    </ul>
    <div class="navpro-mobile"></div>


    <?php if ($styles = $block->getMenu()->getStyles()) : ?>
        <style>
            <?php echo $styles ?>
        </style>
    <?php endif; ?>
</nav>

Leave a comment

Your email address will not be published. Required fields are marked *