Magento 2.2.5: How to remove links on my account navigation by php code?
-
30-01-2021 - |
Frage
What's up guys, I'm looking for how to remove links on "My Account" page in magento 2 with PHP code.
So i've searched a lot and only saw this layout xml solution:
How to add/remove links on my account navigation magento2?
But what i want is do it by PHP code, because i have some special condition, for example, when customer is in wholesale group, then the "My order" is hiding.
The check customer part is an easy part and i've done it, but not with the "hiding my order" part
Any idea? Please help, thanks :)
Lösung
You can try this:
create this file Vendor/Module/etc/frontend/di.xml
with below content.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="\Magento\Framework\View\Element\Html\Links">
<plugin name="vendor_module_element_link" type="\Vendor\Module\Plugin\LinksPlugin" sortOrder="1" />
</type>
</config>
Create LinksPlugin.php
file in /Vendor/Module/Plugin/
with below cpntent.
namespace Vendor\Module\Plugin;
class LinksPlugin
{
public function afterRenderLink(\Magento\Framework\View\Element\Html\Links $subject, $result, \Magento\Framework\View\Element\AbstractBlock $link)
{
if($link->getNameInLayout()=='customer-account-navigation-account-link')
{
$result = "";
}
return $result;
}
}
Andere Tipps
You can create observer for event layout_render_before_customer_account and add logic that necessary to your requirements.
Example how to create Observer located here https://magento.stackexchange.com/a/228406/43911