Magento 2: Add a Custom column to shopping cart table

Add a warehouse location to the shopping cart page and other information about items

Posted on July 18, 2018 in Magento2

First create a Magento Extension

Here, I create a Magento module/extension, named ‘Jeff_Example’. You can search online to find the tutorial how to create a new module.

Create a di.xml file

We are going to create a ‘after plugin’ for the method ‘getItemRenderer’ in ‘Magento\Checkout\Block\Cart\AbstractCart’ class.


#app/code/Jeff/Example/etc/di.xml

<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="Jeff\Example\Block\Cart\AbstractCart" sortOrder="1" />
    </type>
</config>

Create the after plugin


#app/code/Jeff/Example/Block/Cart/AbstractCart.php

<?php
namespace Jeff\Example\Block\Cart;

class AbstractCart
{
    public function afterGetItemRenderer(\Magento\Checkout\Block\Cart\AbstractCart $subject, $result)
    {
        $result->setTemplate('Jeff_Example::cart/item/default.phtml');

        return $result;
    }
}

Create the Template file

Just copy the source file from 'vendor/magento/module-checkout/view/frontend/templates/cart/item/default.phtml' to your module. For me, it's app/code/Jeff/Example/view/frontend/templates/cart/item/default.phtml. And modify this file by adding:

        <td class="col">
            Nj Warehouse location
        </td>

and copy another source file from 'vendor/magento/module-checkout/view/frontend/templates/cart/form.phtml' to your module. And modify this file by adding some heading text.

<th class="col item" scope="col"><span><?php /* @escapeNotVerified */ echo __('Warehouse Location') ?></span></th>

After clean cache and reload your shopping cart page, you will see a new column add to the shopping cart table.

custom linked product


comments powered by Disqus