Magento 2

How to Add Extra New Column on Shopping Cart Page of Magento 2

The shopping cart is like a bridge between shoppers & sellers and It’s hard to brief importance of cart page because ultimately it is one of the biggest choke points from every customer passes through while making a purchase of product or services. That’s why instead of giving more look and feel to the store design, store owner needs to streamlined whole shopping funnel. Also, some store owners redesigned whole cart interface to make the shopping experience easier, convenient and super clear by keeping only important things to cart page.

Having a Magento as a flexible platform, we have optimized several cart pages, to fulfill business needs by delivering optimized and crystal-clear interface to make the whole shopping process as easy as having a sip of coffee. Recently, one of our clients came up with the requirement of adding an extra column to display some product-related remarks to store cart page so the customer can keep things in mind before making a purchase.

Firstly, you need to create ‘AbstractCart.php’ file at below location.
app\code\Vendor\Extension\Block\Cart\AbstractCart.php

<pre class="lang:default decode:true">
<?php
 
namespace Vendor\Extension\Block\Cart;
 
class AbstractCart
{
 
 public function afterGetItemRenderer(\Magento\Checkout\Block\Cart\AbstractCart $subject, $result)
 {
        $result->setTemplate('[vendor\Extension]::cart/item/default.phtml');
     return $result;
 }
}
</pre>

Now you need to create another ‘di.xml’ file inside your extension folder at given location using following location.
app\code\Vendor\Extension\etc\di.xml

<pre class="lang:default decode:true">
<?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\Checkout\Block\Cart\AbstractCart">
     <plugin name="cart-item-override" type="Vendor\Extension\Block\Cart\AbstractCart" sortOrder="1"/>
 </type>
</config>
</pre>

Now you have to copy required code from default Magento cart ‘default.phtml’ file,
vendor\magento\module-checkout\view\frontend\templates\cart\item\default.phtml

to your custom extension ‘default.html’ file created at below path.
app\code\Vendor\Extension\view\frontend\templates\cart\item\default.phtml

Because we are willing to add new column that’s why we will add new here. Beside that you can also add your own custom code as per your requirement.

<pre class="lang:default decode:true">
 <td class="col">
     <textarea rows="4" class="remarks"></textarea>
 </td>
</pre>

Once you have added column, now we need to add specify header using below code inside ‘checkout_cart_index.xml’ file at below location.
app\code\Vendor\Extension\view\frontend\layout\checkout_cart_index.xml

<pre class="lang:default decode:true">
 <?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
     <referenceBlock name="checkout.cart.form">
         <action method="setTemplate">
             <argument name="template" xsi:type="string">Vendor_Extension::cart/form.phtml</argument>
         </action>
     </referenceBlock>
 </body>
</page>
</pre>

Lastly, you have to copy required code from default Magento cart ‘form.phtml’ file,
vendor\magento\module-checkout\view\frontend\templates\cart\form
to your custom extension ‘form.html’ file created at below path and place a column in table sequence to display your column.
app\code\Vendor\Extension\view\frontend\template\cart\form.phtml

<pre class="lang:default decode:true">
             <th class="col item" scope="col"><span><?= /* @escapeNotVerified */ __('Remarks with product') ?></span></th>
</pre>

And that’s it! You can try and explore more by adding and testing various logic according to your requirements of adding column to cart page.
Comment down below if you are looking for any help regarding this code.
Happy Coding!

Click to rate this post!
[Total: 9 Average: 3.9]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago