How To Override Shopping Cart default.phtml by using custom Plugin in Magento 2.x / Adobe Commerce 2.x

The following below steps need to follow to overriding Shopping Cart default.phtml by using custom Plugin in Magento 2.x / Adobe Commerce 2.x

Here we are overriding below shopping cart default.phtml

vendor/magento/module-checkout/view/frontend/templates/cart/item/default.phtml

Step [1] – Nominate Module Name ::

Syntax as below

Namespace_Modulename =Mage2db_John

Magento2 Root Directory / app /code / Mage2db / John

Step [2] – Create a module’s registration file :: This file name is registration.php & used to registered module.

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / registration.php

Write below code in registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mage2db_John',
    __DIR__
);

Step [3] – Create a module’s naming configuration file :: This file name is module.xml & used to define module name

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / etc / module.xml

Write below code in module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mage2db_John" setup_version="1.0.0"/> 
</config>

Step [4] – Create a di.xml file to define Plugin

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / etc / di.xml

Write below code in di.xml to define Plugin

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- override cart/item/default.phtml file -->
    <type name="Magento\Checkout\Block\Cart\AbstractCart">
        <plugin name="item-cart" type="Magedb\John\Plugin\Cart\AbstractCart" sortOrder="1"/>
    </type>
</config>

Step [5] – Create a plugin file

File Path as below

Magento2 Root Directory / app /code / Mage2db / John / Plugin / Cart / AbstractCart.php
<?php
namespace Mage2db\John\Plugin\Cart;
class AbstractCart
{
    /*
    *   Override cart/item/default.phtml file
    *   \Magento\Checkout\Block\Cart\AbstractCart $subject
    *   $result
    */
    public function afterGetItemRenderer(\Magento\Checkout\Block\Cart\AbstractCart $subject, $result)
    {
        $result->setTemplate('Mage2db_John::cart/item/default.phtml');
        return $result;
    }
}

Step [6] – Create a Template file default.phtml below path

app / code / Mage2db / John / view / frontend / templates / cart / item / default.phtml

Step [7] – Finally your custom module by using plugin to do override below default.phtml file has been created

vendor/magento/module-checkout/view/frontend/templates/cart/item/default.phtml

Run the following below commands at your Magento 2 root directory


php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush

Leave a Reply

Your email address will not be published. Required fields are marked *