1

how to add a custom block in cart summary just after subtotal in Magento 2 ?

I have followed many links, but none helped me.

Any help would be appreciated.

2 Answers 2

0

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

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

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

app/code/Mage/Mohit/view/frontend/templates/custom-block.phtml

<?php echo "Custom Block"; ?>

And you make to custom module so create more two file like :-

app/code/Mage/Mohit/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Mage_Mohit',
    __DIR__
);

app/code/Mage/Mohit/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="Mage_Mohit" setup_version="1.2.9"/>
</config>

And see output:-

enter image description here

And custom block show after order total check xml code :-

<?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" template="Mage_Mohit::custom-block.phtml" />
        </referenceContainer>
    </body>
</page>

enter image description here

4
  • I would like it to be below subtotal not above, if possible between subtotal and order total
    – Sharon
    Apr 9, 2020 at 13:50
  • Custom Block show after order total and check my update answer. So helpful this answer so accept and upvote answer because useful to other community Thanks. Apr 9, 2020 at 17:01
  • thank you for your answer but my problem is that I would like the custom block to be between subtotal and order total
    – Sharon
    Apr 10, 2020 at 6:19
  • @MohitPatel, did you get solution to move after sub total?
    – Manjunath
    Oct 3, 2021 at 10:45
0

You can add a custom template just below the "Subtotal" on the Cart page summary. Just need to follow this code -

NOTE - Don't forget to add sortOrder

<referenceBlock name="checkout.cart.totals">
    <arguments>
        <argument name="jsLayout" xsi:type="array">
            <item name="components" xsi:type="array">
                <item name="block-totals" xsi:type="array">
                    <item name="children" xsi:type="array">
                        <!-- Add Custom Template -->
                        <item name="custom_template_below_subtotal" xsi:type="array">
                            <item name="component" xsi:type="string">Magento_Tax/js/view/checkout/summary/custom_template_below_subtotal</item>
                            <item name="sortOrder" xsi:type="string">20</item>
                            <item name="config" xsi:type="array">
                                <item name="template" xsi:type="string">Magento_Tax/checkout/summary/custom_template_below_subtotal</item>
                            </item>
                        </item>
                        <!-- Add Custom Template -->
                    </item>
                </item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

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.