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

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago