Magento2 Cart Page – How to Show Custom Block

blockscartmagento2

Namespacee/Modulename/Block/Productpoint.php

<?php

namespace Namespace\Modulename\Block;

class Productpoint extends \Magento\Framework\View\Element\Template
{

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }


}

Namespace/Modulename/view/frontend/layout/checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <body>
        <referenceContainer name="content">
            <block template="checkout/cart/productpoint.phtml" class="Namespace\Modulename\Block\Productpoint" name="productpoints.cart.block" as="productpointblock" before="-"/>
        </referenceContainer>
    </body>
</page>

Namespace/Modulename/view/frontend/templates/checkout/cart/productpoint.phtml

<span><?php echo 'Eureka'; ?></span>

The block is not loading on cart page. How do I call my custom block on cart page ?

Best Answer

Rewrite 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="content">
            <block class="Namespace\Modulename\Block\Productpoint" before="-" template="Namespace_Modulename::checkout/cart/productpoint.phtml"/>
        </referenceContainer>
    </body>
</page>

Now the block will be displayed on top of shopping cart.