Magento 2 – Adicionando um link no menu minha conta.

Como adicionar um link no menu minha conta, que está presente na página para aonde o cliente é redirecionando quando se autentica no Magento 2.

Em seu módulo, crie o arquivo customer_account.xml na seguinte localização

Namespace/ModuleName/view/frontend/layout

O conteúdo do arquivo customer_account.xml está no final do post, na sequência temos todo o código necessário para criar o módulo.

Criando o módulo

Substitua o [Vendor] e [ModuleName]…

app/code/[Vendor]/[ModuleName]/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="[Vendor]_[ModuleName]" setup_version="0.0.1" active="true"/>
</config>

app/code/[Vendor]/[ModuleName]/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 '[Vendor]_[ModuleName]',
 __DIR__
);

app/code/[Vendor]/[ModuleName]/etc/frontend/routes.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
 <router id="standard">
 <route id="[modulename]" frontName="[modulename]">
 <module name="[Vendor]_[ModuleName]"/>
 </route>
 </router>
</config>

app/code/[Vendor]/[ModuleName]/Controller/Index/Index.php

<?php

namespace [Vendor]\[ModuleName]\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{

 protected $_resultPageFactory;

 public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
 {
 $this->_resultPageFactory = $resultPageFactory;
 parent::__construct($context);
 }

 public function execute()
 {

 $resultPage = $this->_resultPageFactory->create();

 return $resultPage;

 }
}

app/code/[Vendor]/[ModuleName]/view/frontend/layout/[modulename]_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="2columns-left">
 <update handle="customer_account"/>
 <referenceBlock name="page.main.title">
 <action method="setPageTitle">
 <argument translate="true" name="title" xsi:type="string">[My Title]</argument>
 </action>
 </referenceBlock>
 <referenceContainer name="page.messages">
 <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages"/>
 </referenceContainer>
 <referenceContainer name="content">
 <block class="[Vendor]\[ModuleName]\Block\MyBlock" name="[modulename]_[modulename]_index.view" template="[Vendor]_[ModuleName]::index.phtml"/>
 </referenceContainer>
</page>

app/code/[Vendor]/[ModuleName]/Block/MyBlock.php

<?php
namespace [Vendor]\[ModuleName]\Block;

class MyBlock extends \Magen
 public $_storeManto\Framework\View\Element\Template
{ager;
 /**
 * Construct
 *
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param array $data
 */
 public function __construct(
 \Magento\Backend\Block\Template\Context $context, 
 \Magento\Store\Model\StoreManagerInterface $storeManager
 )
 {
 $this->_storeManager=$storeManager;
 parent::__construct($context);
 }

 public function _prepareLayout()
 {
 $this->pageConfig->getTitle()->set(__('My Block'));

 return parent::_prepareLayout();
 }
}

app/code/[Vendor]/[ModuleName]/view/frontend/layout/customer_account.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">
 <body>
 <referenceBlock name="customer_account_navigation">
 <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer_account_my_link">
 <arguments>
 <argument name="path" xsi:type="string">[modulename]/index/index</argument>
 <argument name="label" xsi:type="string">My Link</argument>
 </arguments>
 </block>
 </referenceBlock>
 </body>
</page>

Remova o diretório var e limpe o Cache.

2017-06-12T17:56:52+00:00

RECEBA DICAS VALIOSAS NO SEU EMAIL

x