5

I would like to show the customer dashboard "Pay trade customer link" to particular user (Ex: example@gmail.com) i have followed this link Add New inbox tab in customer my dashboard in magento 2 for adding the custom link in the customer dashboard. hence i have created a new custom module and did the changes accordingly.

Please find the screenshot:

enter image description here

Please suggest me how i can do this.

5

please check the below code.

step 1 path - app/code/Training/Msg/registration.php

<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE,'Training_Msg',__DIR__);

step 2 path - app/code/Training/Msg/etc/module.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Training_Msg" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

step 3 path - app/code/Training/Customerlink/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Framework\View\Element\Html\Link\Current" type="Training\Msg\Block\Current"/>
</config>

step 4 path - app/code/Training/Msg/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="msg" frontName="msg">
            <module name="Training_Msg"/>
        </route>
    </router>
</config>

step 5 path - app/code/Training/Msg/Controller/Index/Msg.php

<?php
namespace Training\Msg\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\PageFactory;

class Msg extends Action
{
    private $jsonFactory;

    private $pageFactory;


    public function __construct(
        Context $context,
        JsonFactory $jsonFactory,
        PageFactory $pageFactory
    ){
        $this->jsonFactory = $jsonFactory;
        $this->pageFactory = $pageFactory;

        parent::__construct($context);
    }

    public function execute()
    {
        //$result = $this->jsonFactory->create();
        $result = $this->pageFactory->create();
        $result->getConfig()->getTitle()->set("Welcome Msg");
        //$data = ['msg' => 'tets'];
        //return $result->setData($data);
        return $result;
    }

}

step 6 path - app/code/Training/Msg/Block/Current.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Training\Msg\Block;

/**
 * Block representing link with two possible states.
 * "Current" state means link leads to URL equivalent to URL of currently displayed page.
 *
 * @method string                          getLabel()
 * @method string                          getPath()
 * @method string                          getTitle()
 * @method null|array                      getAttributes()
 * @method null|bool                       getCurrent()
 * @method \Magento\Framework\View\Element\Html\Link\Current setCurrent(bool $value)
 */
class Current extends \Magento\Framework\View\Element\Template
{
    /**
     * Default path
     *
     * @var \Magento\Framework\App\DefaultPathInterface
     */
    protected $_defaultPath;

    protected $customerSession;

    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\App\DefaultPathInterface $defaultPath
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\DefaultPathInterface $defaultPath,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_defaultPath = $defaultPath;
        $this->customerSession = $customerSession;
    }

    /**
     * Get href URL
     *
     * @return string
     */
    public function getHref()
    {
        return $this->getUrl($this->getPath());
    }

    /**
     * Get current mca
     *
     * @return string
     */
    private function getMca()
    {
        $routeParts = [
            'module' => $this->_request->getModuleName(),
            'controller' => $this->_request->getControllerName(),
            'action' => $this->_request->getActionName(),
        ];

        $parts = [];
        foreach ($routeParts as $key => $value) {
            if (!empty($value) && $value != $this->_defaultPath->getPart($key)) {
                $parts[] = $value;
            }
        }
        return implode('/', $parts);
    }

    /**
     * Check if link leads to URL equivalent to URL of currently displayed page
     *
     * @return bool
     */
    public function isCurrent()
    {
        return $this->getCurrent() || $this->getUrl($this->getPath()) == $this->getUrl($this->getMca());
    }

    /**
     * Render block HTML
     *
     * @return string
     */
    protected function _toHtml()
    {

        if (false != $this->getTemplate()) {
            return parent::_toHtml();
        }

        $highlight = '';

        if ($this->getIsHighlighted()) {
            $highlight = ' current';
        }

        if ($this->isCurrent()) {
            $html = '<li class="nav item current">';
            $html .= '<strong>'
                . $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()))
                . '</strong>';
            $html .= '</li>';
        } else {
            if( $this->customerSession->getCustomerGroupId() == 1 ){
            if ( strpos($this->getHref(),'msg') == false ) {
            $html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
            $html .= $this->getTitle()
                ? ' title="' . $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getTitle())) . '"'
                : '';
            $html .= $this->getAttributesHtml() . '>';

            if ($this->getIsHighlighted()) {
                $html .= '<strong>';
            }

            $html .= $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()));

            if ($this->getIsHighlighted()) {
                $html .= '</strong>';
            }

            $html .= '</a></li>';
        }
        }else
        {
            $html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
            $html .= $this->getTitle()
                ? ' title="' . $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getTitle())) . '"'
                : '';
            $html .= $this->getAttributesHtml() . '>';

            if ($this->getIsHighlighted()) {
                $html .= '<strong>';
            }

            $html .= $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()));

            if ($this->getIsHighlighted()) {
                $html .= '</strong>';
            }

            $html .= '</a></li>';
        }
    }
        return $html;
}

    /**
     * Generate attributes' HTML code
     *
     * @return string
     */
    private function getAttributesHtml()
    {
        $attributesHtml = '';
        $attributes = $this->getAttributes();
        if ($attributes) {
            foreach ($attributes as $attribute => $value) {
                $attributesHtml .= ' ' . $attribute . '="' . $this->escapeHtml($value) . '"';
            }
        }

        return $attributesHtml;
    }
}

Here the custom link Customer Message only display to user group id is not equal to 1.

step 7 path - app/code/Training/Msg/Block/Msg.php

<?php
namespace Training\Msg\Block;

use Magento\Framework\View\Element\Template;

class Msg extends Template
{
    public function __construct(Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function getTitle()
    {
        return "Test Message";
    }
}

step 8 path - app/code/Training/Msg/view/frontend/layout/customer_account.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-myemail">
                <arguments>
                    <argument name="path" xsi:type="string">msg/index/msg</argument>
                    <argument name="label" xsi:type="string">Customer Message</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
 </page>

step 9 path - app/code/Training/Msg/view/frontend/layout/msg_index_msg.xml

<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
      <update handle="customer_account"/>
    <body>
        <referenceBlock name="content">
        <block name="msg" class="Training\Msg\Block\Msg" template="Training_Msg::msg.phtml" />
        </referenceBlock>
    </body>
</page>

step 10 path - app/code/Training/Msg/view/frontend/templates/msg.phtml

<?php echo $block->getTitle(); ?>

OR

You can download the module from github link https://github.com/jothibasuj/customer-dashboard-link-based-on-condition

  1. Run setup upgrade.
  2. Clear cache.
  3. set permission to var folder.
  • Hi you can try the above code – Jjo Jun 29 '17 at 9:11
  • sure jothibasu i will update you thanks for u r reply – Nagaraju K Jun 29 '17 at 9:55
  • hi i am getting awesomescreenshot.com/image/2621641/… – Nagaraju K Jun 29 '17 at 10:49
  • i added some print statement in Current.php for checking purpose. now updated the code. – Jjo Jun 29 '17 at 10:55
  • @NAGARAJUKASA - Did you check working or not? – Jjo Jun 29 '17 at 13:02
3

First You need create block extents class Magento\Framework\View\Element\Html\Link\Current

your block code like

namespace Namespace\Module\Block\View\Element\Html\Link;

use Magento\Customer\Model\Session as CustomerSession;

class Current extends \Magento\Framework\View\Element\Html\Link\Current{

    protected $_customerSession;
    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\App\DefaultPathInterface $defaultPath
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\DefaultPathInterface $defaultPath,
        CustomerSession $CustomerSession,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_defaultPath = $defaultPath;
        $this->_customerSession = $CustomerSession;
    }


    public function _toHtml(){
          // Here you can check the conditions.
          if($this->_customerSession->getCustomer()->getCustomAttribute()){
               return parent::_toHtml();
          }
          return '';
    }  
}
  • Thanks for your answer i will check and get back @jothbasu – Nagaraju K Jun 28 '17 at 9:00
  • @NAGARAJUKASA - Did you check the above sample code? – Jjo Jun 28 '17 at 10:14
  • hi @hothibasu that paytrade link how to show for the particular user? – Nagaraju K Jun 28 '17 at 10:28
  • @NAGARAJUKASA - you can add condition in the toHtml function. please refer the above answer – Jjo Jun 28 '17 at 10:30
  • hi @jothibasu i have configured my class in using preference in di.xml also but its not working – Nagaraju K Jun 28 '17 at 12:05

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.