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:
-
Create a new folder under
app/codenamedCompany_CartLink. -
Inside this folder, create two more folders:
etcandregistration. -
In the
etcfolder, create a file nameddi.xml. This will contain our event observer configuration. -
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>
-
Create the file resolver class at
app/code/Company/CartLink/Model/Template/FileResolver.php. This class will extend\Magento\Framework\View\Asset\FileResolverInterfaceand return our custom XML file paths. -
In your module’s
etcfolder, create a new folder namedevents. -
Inside the
eventsfolder, create a file namedtoplinks.xml. This is where we’ll define our event observer. -
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>
-
Create the
TopLinksObserverclass atapp/code/Company/CartLink/Observer/TopLinksObserver.php. This observer will add our shopping cart link to the top navigation. -
In the
TopLinksObserverclass, implement theexecutemethod 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.