how to add shopping cart url under top links in magento through customer.xml

how to add shopping cart url under top links in magento through customer.xml

October 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to add shopping cart url under top links in magento through customer.xml

Adding Shopping Cart URL Under Top Links in Magento: A Comprehensive Guide

As an experienced e-commerce developer, I understand how challenging it can be to customize the top navigation links in Magento. One of the most common requests is adding the shopping cart URL directly under the main menu.

You’re on the right track by trying to modify the customer.xml file to achieve this. The top.links reference block is where you need to make your changes.

The Incorrect Approach: Direct Modification

Your current approach involves directly adding an action inside the top.links reference in customer.xml. While this might seem straightforward, it’s not the recommended way to add a new link in Magento.

The issue with this method is that it modifies the core file, which can lead to conflicts during upgrades. Plus, it doesn’t follow the proper configuration structure for adding links in Magento 2.

The Correct Way: Using Event Observer

Instead of directly modifying core files, you should use an event observer to add your shopping cart link. This approach keeps your customizations separate from the core and ensures they won’t be overwritten during updates.

First, let’s create a new module for our shopping cart link:

  1. Create a new folder under app/code named Company_CartLink.

  2. Inside this folder, create two more folders: etc and registration.

  3. In the etc folder, create a file named di.xml. This will contain our event observer configuration.

  4. Add the following code to di.xml:

<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\Template\Context">
        <arguments>
            <argument name="templateFileResolver" xsi:type="object">Company\CartLink\Model\Template\FileResolver</argument>
        </arguments>
    </type>
</config>
  1. Create the file resolver class at app/code/Company/CartLink/Model/Template/FileResolver.php. This class will extend \Magento\Framework\View\Asset\FileResolverInterface and return our custom XML file paths.

  2. In your module’s etc folder, create a new folder named events.

  3. Inside the events folder, create a file named toplinks.xml. This is where we’ll define our event observer.

  4. Add the following code to toplinks.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Ace/config.xsd">
    <event name="Magento_Theme_Block_Html_Topmenu_GetHtmlBefore">
        <observer name="company_add_shopping_cart_link" instance="Company\CartLink\Observer\TopLinksObserver"/>
    </event>
</config>
  1. Create the TopLinksObserver class at app/code/Company/CartLink/Observer/TopLinksObserver.php. This observer will add our shopping cart link to the top navigation.

  2. In the TopLinksObserver class, implement the execute method and use it to inject your shopping cart link into the top menu:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $block = $observer->getBlock();
    $link = [
        'label' => __('Shopping Cart'),
        'url' => $this->_checkoutHelper->getCheckoutUrl(),
        'position' => 10,
    ];
    $block->addLink($link);
}

Remember to replace Company\CartLink with your actual module name and use the appropriate helper classes for retrieving the shopping cart URL.

Conclusion

While it might seem easier to modify core XML files directly, doing so can lead to upgrade conflicts and makes your codebase more difficult to maintain. By using an event observer approach, you keep your customizations clean and separate from the Magento core.

This method ensures that your link remains intact even after upgrading to a new version of Magento. It also keeps your code organized within its own module structure.

Don’t forget to enable your module in the Magento backend (Stores > Configuration > Advanced > Advanced). For more detailed guides on Magento development, feel free to explore our blog at https://laravelcompany.com where we regularly provide insights and tutorials on e-commerce development.

Magento 2 Checkout Extensions Configuration

Our network