How to override module-checkout/.../cart/item/default.phtml?
you can do it using override in your module with below way,
You must have to change referencename of your xml,
First create app/code/Custom/Addprofile/view/frontend/layout/checkout_cart_index.xml,
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock class="Magento\Checkout\Block\Cart" name="checkout.cart.form">
<block class="Magento\Framework\View\Element\RendererList" name="checkout.cart.item.renderers.override" as="renderer.list.custom"/>
<arguments>
<argument name="renderer_list_name" xsi:type="string">checkout.cart.item.renderers.override</argument>
</arguments>
</referenceBlock>
</body>
</page>
Now your checkout_cart_item_renderers.xml
<?xml version="1.0"?>
<page 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.item.renderers.override">
<block class="Magento\Checkout\Block\Cart\Item\Renderer" as="default" template="Custom_Addprofile::cart/item/default.phtml" />
<block class="Magento\Checkout\Block\Cart\Item\Renderer" as="simple" template="Custom_Addprofile::cart/item/default.phtml" />
</referenceBlock>
</body>
</page>
Finally after three days. I got it working. Below is the correct way of overriding it.
app\code\Vendor\Module\Block\Cart\AbstractCart.php
<?php
/**
* Override cart item block in custom module
*/
namespace Vendor\Custom\Block\Cart;
class AbstractCart
{
public function afterGetItemRenderer(\Magento\Checkout\Block\Cart\AbstractCart $subject, $result)
{
$result->setTemplate('Vendor_Custom::cart/item/default.phtml');
return $result;
}
}
app\code\Vendor\Module\etc\di.xml
<!--To override cart template-->
<type name="Magento\Checkout\Block\Cart\AbstractCart">
<plugin name="cart-item-override" type="Vendor\Custom\Block\Cart\AbstractCart" sortOrder="1"/>
</type>
app\code\Vendor\Custom\view\frontend\cart\item\default.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var $block \Magento\Checkout\Block\Cart\Item\Renderer */
die('template overridden');
$_item = $block->getItem();
$product = $_item->getProduct();
$isVisibleProduct = $product->isVisibleInSiteVisibility();
/** @var \Magento\Msrp\Helper\Data $helper */
$helper = $this->helper('Magento\Msrp\Helper\Data');
$canApplyMsrp = $helper->isShowBeforeOrderConfirm($product) && $helper->isMinimalPriceLessMsrp($product);
?>
<tbody class="cart item">
<tr class="item-info">
<td data-th="<?= $block->escapeHtml(__('Item')) ?>" class="col item">
<?php if ($block->hasProductUrl()):?>
<a href="<?= /* @escapeNotVerified */ $block->getProductUrl() ?>"
title="<?= $block->escapeHtml($block->getProductName()) ?>"
tabindex="-1"
class="product-item-photo">
<?php else:?>
<span class="product-item-photo">
<?php endif;?>
<?= $block->getImage($block->getProductForThumbnail(), 'cart_page_product_thumbnail')->toHtml() ?>
<?php if ($block->hasProductUrl()):?>
</a>
<?php else: ?>
</span>
<?php endif; ?>
<div class="product-item-details">
<strong class="product-item-name">
<?php if ($block->hasProductUrl()):?>
<a href="<?= /* @escapeNotVerified */ $block->getProductUrl() ?>"><?= $block->escapeHtml($block->getProductName()) ?></a>
<?php else: ?>
<?= $block->escapeHtml($block->getProductName()) ?>
<?php endif; ?>
</strong>
<p><strong>Note: </strong>This is a renewal prodcut.</p>
<?php if ($_options = $block->getOptionList()):?>
<dl class="item-options">
<?php foreach ($_options as $_option) : ?>
<?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
<dt><?= $block->escapeHtml($_option['label']) ?></dt>
<dd>
<?php if (isset($_formatedOptionValue['full_view'])): ?>
<?= /* @escapeNotVerified */ $_formatedOptionValue['full_view'] ?>
<?php else: ?>
<?= /* @escapeNotVerified */ $_formatedOptionValue['value'] ?>
<?php endif; ?>
</dd>
<?php endforeach; ?>
</dl>
<?php endif;?>
<?php if ($messages = $block->getMessages()): ?>
<?php foreach ($messages as $message): ?>
<div class="cart item message <?= /* @escapeNotVerified */ $message['type'] ?>"><div><?= $block->escapeHtml($message['text']) ?></div></div>
<?php endforeach; ?>
<?php endif; ?>
<?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
<?php if ($addInfoBlock): ?>
<?= $addInfoBlock->setItem($_item)->toHtml() ?>
<?php endif;?>
</div>
</td>
<?php if ($canApplyMsrp): ?>
<td class="col msrp" data-th="<?= $block->escapeHtml(__('Price')) ?>">
<span class="pricing msrp">
<span class="msrp notice"><?= /* @escapeNotVerified */ __('See price before order confirmation.') ?></span>
<?php $helpLinkId = 'cart-msrp-help-' . $_item->getId(); ?>
<a href="#" class="action help map" id="<?= /* @escapeNotVerified */ ($helpLinkId) ?>" data-mage-init='{"addToCart":{"helpLinkId": "#<?= /* @escapeNotVerified */ $helpLinkId ?>","productName": "<?= /* @escapeNotVerified */ $product->getName() ?>","showAddToCart": false}}'>
<span><?= /* @escapeNotVerified */ __("What's this?") ?></span>
</a>
</span>
</td>
<?php else: ?>
<td class="col price" data-th="<?= $block->escapeHtml(__('Price')) ?>">
<?= $block->getUnitPriceHtml($_item) ?>
</td>
<?php endif; ?>
<td class="col qty" data-th="<?= $block->escapeHtml(__('Qty')) ?>">
<div class="field qty">
<label class="label" for="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty">
<span><?= /* @escapeNotVerified */ __('Qty') ?></span>
</label>
<div class="control qty">
<input id="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty"
name="cart[<?= /* @escapeNotVerified */ $_item->getId() ?>][qty]"
data-cart-item-id="<?= /* @escapeNotVerified */ $_item->getSku() ?>"
value="<?= /* @escapeNotVerified */ $block->getQty() ?>"
type="number"
size="4"
title="<?= $block->escapeHtml(__('Qty')) ?>"
class="input-text qty"
data-validate="{required:true,'validate-greater-than-zero':true}"
data-role="cart-item-qty"/>
</div>
</div>
</td>
<td class="col subtotal" data-th="<?= $block->escapeHtml(__('Subtotal')) ?>">
<?php if ($canApplyMsrp): ?>
<span class="cart msrp subtotal">--</span>
<?php else: ?>
<?= $block->getRowTotalHtml($_item) ?>
<?php endif; ?>
</td>
</tr>
<tr class="item-actions">
<td colspan="100">
<div class="actions-toolbar">
<?= /* @escapeNotVerified */ $block->getActions($_item) ?>
</div>
</td>
</tr>
</tbody>
Thanks
Here is the correct way to do an override the default.phtml.
First, you need to write sequence in module.xml. Without these sequences, you can't override.
app/code/VendorName/ModuleName/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="VendorName_ModuleName" setup_version="0.0.1">
<sequence>
<module name="Magento_Checkout" />
<module name="Magento_Bundle" />
<module name="Magento_Catalog" />
<module name="Magento_ConfigurableProduct" />
<module name="Magento_Downloadable" />
<module name="Magento_GroupedProduct" />
</sequence>
</module>
On the next step you should set your template in checkout_cart_item_renderers.xml
app/code/VendorName/ModuleName/view/frontend/layout/checkout_cart_item_renderers.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.cart.item.renderers.bundle" template="VendorName_ModuleName::cart/item/default.phtml" />
<referenceBlock name="checkout.cart.item.renderers.virtual" template="VendorName_ModuleName::cart/item/default.phtml" />
<referenceBlock name="checkout.cart.item.renderers.default" template="VendorName_ModuleName::cart/item/default.phtml" />
<referenceBlock name="checkout.cart.item.renderers.simple" template="VendorName_ModuleName::cart/item/default.phtml" />
<referenceBlock name="checkout.cart.item.renderers.configurable" template="VendorName_ModuleName::cart/item/default.phtml" />
<referenceBlock name="checkout.cart.item.renderers.grouped" template="VendorName_ModuleName::cart/item/default.phtml" />
</body>
</page>
And last step it is your template which should be in this path.
app/code/VendorName/ModuleName/view/frontend/templates/cart/item/default.phtml
Enjoy!