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 :)
-
you can remove whole sidebar and create custom sidebar then after you can do whatever you want.– Aasim GoriyaOct 5, 2018 at 11:31
-
you can try plugin– kunjOct 5, 2018 at 12:28
-
@AasimGoriya that sound cool but won't be the best practice :) I hope to find another way :)– fuduOct 5, 2018 at 13:12
2 Answers
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;
}
}
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
-
thanks for replying, but if i use observer, then how to hide the "My Order" (which is the links that i want to hiding)?– fuduOct 5, 2018 at 13:09