In my main nav I have link and the url changes based upon if the customer is logged in. If the customer is login in the link is /shop/trade else the link is /trade.

My code is the following:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
    echo '/shop/trade';
    echo '/trade';

The issue is though the block is being cached and thus the user does not see the updated link when logged in. I looked at using cacheable="false" on the block but I learnt this will disable caching on all pages since the nav is on all pages.

Whats the best way to sort this?

  • So basically you are changing all the links based on whether the client is logged in or not? Feb 19 '18 at 15:33
  • You can do this by js.
    – Sohel Rana
    Feb 19 '18 at 15:45

Create a js file:


], function ($, ko, Component, customerData, $t) {
    'use strict';

    return Component.extend({
        isLoggedIn: ko.observable(true),
        customer: ko.observable({}),

        initialize: function() {

         * Check customer localstorage
        checkCustomerLocalStorage: function () {
            var self = this;
            var time = setInterval(function () {
                self.customer = customerData.get('customer');

                if (localStorage["mage-cache-storage"] != '{}') {

                if (self.customer().fullname != undefined) {
                    // show your login link
                    // $("a.mylink").attr("href", "/shop/trade");
                } else {
                    // show your logout link
                    // $("a.mylink").attr("href", "/trade");
            }, 1000);

Inside phtml file:

<script type="text/x-magento-init">
    "*": {

You should use knockout to load generate this content dynamically.

Smth like this should work:

<div data-bind="scope: 'customer'">

        <a style="display: none" data-bind="visible: (customer().fullname)" href="#authorized">authorized link</a>
        <a style="display: none" data-bind="visible: (!customer().fullname)"href="#guest">guest link</a>
<script type="text/x-magento-init">
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "customer": {
                        "component": "Magento_Customer/js/view/customer"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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