Moving the “My Cart” Link Outside of Top Links in Magento
Hello! I understand how overwhelming it can be when you’re just starting to work with Magento. Don’t worry, we’ll get this sorted out together.
Firstly, let me assure you that moving the “My Cart” link outside of the top links is absolutely possible and quite common for e-commerce sites. The key lies in understanding how Magento’s template system works.
Magento uses a layered design pattern for its templates. The top navigation bar is defined in the app/design/frontend/<vendor>/<theme>/template/page/html/toplinks.phtml file. You’ll find that this file contains the logic for generating all the links in the top navigation bar, including “My Cart”.
Now, we don’t want to modify this file directly as it could be overwritten during an upgrade. Instead, we’ll create a custom layout XML file.
Step 1: Create a Custom Layout XML File
Navigate to your theme’s app/design/frontend/<vendor>/<theme>/layout folder and create a new XML file named catalog.xml.
<!-- app/design/frontend/<vendor>/<theme>/layout/catalog.xml -->
<layout version="0.1.0">
<default>
<!-- Move My Cart to the Top Right Corner -->
<referenceBlock name="top.links" remove="true"/>
<referenceBlock name="header.links">
<block type="checkout/cart_link" name="cart" as="cart_link" template="checkout/cart/link.phtml"/>
</referenceBlock>
</default>
</layout>
In this file, we’re first removing the entire top links section (top.links) and then adding a new block (header.links). This block contains our “My Cart” link.
Step 2: Customize the My Cart Link
The checkout/cart/link.phtml is the template used for rendering the cart link. You can customize this file by adding your own CSS styles to position it in the top right corner.
<!-- app/design/frontend/<vendor>/<theme>/template/checkout/cart/link.phtml -->
<div class="top-cart">
<a href="<?php echo $this->getUrl('checkout/cart') ?>" title="<?php echo $this->__('My Cart') ?>">
<span class="counter"><?php echo $this->helper('checkout/session')->getCartSession()->getSummaryQuantity() ?></span>
</a>
</div>
In this template, we’re wrapping the “My Cart” link inside a top-cart div. You can apply your custom CSS styles to position it correctly.
Step 3: Add Custom CSS
Add the following CSS to your theme’s skin/frontend/<vendor>/<theme>/css/styles.css file:
.top-cart {
float: right;
margin-right: 20px; /* Adjust as needed */
}
.top-cart span.counter {
background-color: #ff6947; /* Change the color if desired */
display: inline-block;
height: 18px;
line-height: 15px;
padding: 0 3px;
width: 20px;
text-align: center;
}
This CSS will position your “My Cart” link in the top right corner with a custom background color for the counter.
Remember to clear your cache after making these changes using php bin/magento cache:clean.
That’s it! Your “My Cart” link should now appear next to the shopping bag icon in the top right corner of your Magento site, just like on Next.co.uk.
If you’re looking for more advanced customization options or need help with other Magento development tasks, feel free to check out our Magento Development Services at Laravel Company. We specialize in helping businesses like yours build and optimize their e-commerce platforms.
Happy coding! Let me know if you have any more questions.