2

How to add a custom block within checkout cart summary in magento 2

enter image description here

0

3 Answers 3

5

You need to create this file in your custom module/theme to add custom Block on cart page.

app/code/Vendor/Module/view/frontend/layout/checkout_cart_index.xml

Content for this file is..

<?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>
        <referenceContainer name="checkout.cart.totals.container">
            <block class="Magento\Framework\View\Element\Template" name="checkout.cart.custom.block" before="checkout.cart.totals" template="Vendor_Module::custom-block.phtml" />
        </referenceContainer>
    </body>
</page>

You need to create one template file here in your module or your theme.

app/code/Vendor/Module/view/frontend/templates/custom-block.phtml

Content for this file..

<?php echo "Custom Block"; ?>

Output :

enter image description here

Hope this will help you!

5
  • Let me know if this solution works for you. Nov 4, 2019 at 11:29
  • 1
    ya its working. thank you Nov 4, 2019 at 11:31
  • You're always most welcome Buddy! :) Nov 4, 2019 at 11:31
  • hi, I have used the above code but the issue is that it gets added multiple times. By multiple times I meant is that when the second product is added to cart then it displays twice.. Any idea why this is happening..
    – Arjun
    Apr 10, 2020 at 4:22
  • @KishanSavaliya, how to add this after the sub total?
    – Manjunath
    Oct 3, 2021 at 10:53
3

You need to create layout file checkout_cart_index.xml at below path and paste this below code :

app/code/VendorName/ModuleName/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>
    <referenceContainer name="cart.summary">
        <block name="summary.custom.block" class="Magento\Framework\View\Element\Template"  template="VendorName_ModuleName::temp.phtml" after="checkout.cart.shipping" />
    </referenceContainer>
</body>
</page>

Add this phtml file :

app/code/VendorName/ModuleName/view/frontend/templates/temp.phtml

<?php echo __("Hello"); ?>

Cache clean and refresh the page.

1
  • how to move this after subtotal?
    – Manjunath
    Oct 3, 2021 at 10:52
0

Just override checkout_cart_index.xml file and add your custom block using after or move keyword

2
  • welcome to Magento SE. Please explain a bit more while answering, so that people can understand it better. Nov 4, 2019 at 12:22
  • Sure Sir , Thanks Jun 23, 2020 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.