How to remove links from top.liknks when loggin in

2019-02-15 18:32发布

问题:

I've had a good search around and found the customer_logged_in and customer_logged_out tags but I can't get them to work properly, this I'm sure is down to my misunderstanding of them.

My goal is to hide the log in, account and checkout links from the block top.links when the user is not logged in and show them when the user is logged in.

I have placed the following at the end of my local.xml file to try and remove the log in link:

    <customer_logged_in>
        <reference name="top.links">
            <action method="removeLinkByUrl"><url helper="customer/getLoginUrl"/></action>
        </reference>
    </customer_logged_in>

</default>

But it does not work. Can someone please explain why this does not work? It's driving me crazy!

回答1:

Removing links in local.xml works in my case. Here is the code:

<customer_logged_in>
    <reference name="top.links">
        <action method="removeLinkByUrl"><url helper="customer/getLogoutUrl"/></action>
    </reference>
</customer_logged_in>
<customer_logged_out>
    <reference name="top.links">
        <action method="removeLinkByUrl"><url helper="customer/getLoginUrl"/></action>
    </reference>
</customer_logged_out>


回答2:

Ok I found the reason, first off they are top level tags and shouldn't be put in the default tag and secondly it just wasn't working in the local.xml so I placed the code at the top of the customer.xml and it works a treat. Note: I removed the xml which added links to the top.links as well as this seems to interfere with it.

Working xml:

<!--
Load this update on every page when customer is logged in
-->

    <customer_logged_in>
        <reference name="top.links">
            <action method="addLink" translate="label title" module="checkout"><label>My Cart</label><url helper="checkout/cart/getCartUrl"/><title>My Cart</title><prepare/><urlParams/><position>9</position></action>
            <action method="addLink" translate="label title" module="customer"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position></action>
            <action method="addLink" translate="label title" module="customer"><label>Log Out</label><url helper="customer/getLogoutUrl"/><title>Log Out</title><prepare/><urlParams/><position>100</position></action>
        </reference>
    </customer_logged_in>

<!--
Load this update on every page when customer is logged out
-->
    <customer_logged_out>
        <reference name="top.links">
            <action method="addLink" translate="label title" module="customer"><label>Sign up</label><url helper="customer/getRegisterUrl"/><title>Register </title><prepare/><urlParams/><position>100</position></action>
            <action method="addLink" translate="label title" module="customer"><label>Log In</label><url helper="customer/getLoginUrl"/><title>Log In</title><prepare/><urlParams/><position>100</position></action>
            <action method="removeLinkByUrl"><url helper="checkout/url/getCheckoutUrl"/></action>
            <action method="removeLinkByUrl"><url helper="checkout/cart/getCartUrl"/></action>         
            <action method="removeLinkByUrl"><url helper="customer/getLogoutUrl"/></action>
        </reference>
    </customer_logged_out>