Magento 2: Custom block doesn't show on the cart page
-
04-10-2020 - |
Question
Namespace/Modulename/Block/donation.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/web/templates/checkout/cart/donation.phtml
<span><?php echo 'Eureka'; ?></span>
Rewrite checkout_cart_index.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>
<referenceContainer name="content">
<block class="Namespace\Modulename\Block\Productpoint" before="-" template="Namespace_Modulename::checkout/cart/productpoint.phtml"/>
</referenceContainer>
</body>
</page>
The block is not loading on cart page. How do I call my custom block on cart page?
Solution
First of all, your phtml is wrong path, remove web
folder, it should be: Namespace/Modulename/view/frontend/templates/checkout/cart/donation.phtml
Second, Your block is wrong name: Namespace/Modulename/Block/donation.php
=> Namespace/Modulename/Block/Productpoint.php
Third, you can add your block to the containers such items and no items or additional block info. Your template will be rendered automatically.
And your template="Namespace_Modulename::checkout/cart/productpoint.phtml
should be checkout/cart/donation.phtml
.
checkout_cart_index.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>
<referenceContainer name="with-items">
<block class="Namespace\Modulename\Block\Productpoint" name="custom.block" before="-"
template="Namespace_Modulename::checkout/cart/donation.phtml"/>
</referenceContainer>
<referenceContainer name="no-items">
<block class="Namespace\Modulename\Block\Productpoint" name="custom.block" before="-"
template="Namespace_Modulename::checkout/cart/donation.phtml"/>
</referenceContainer>
<referenceBlock name="additional.product.info">
<block class="Namespace\Modulename\Block\Productpoint" name="custom.block" before="-"
template="Namespace_Modulename::checkout/cart/donation.phtml"/>
</referenceBlock>
</body>
</page>
OTHER TIPS
You have put your phtml file at wrong place,Follow Below path
Namespace/Modulename/view/frontend/templates/checkout/cart/donation.phtml After doing so clear your cache and run static content deploy. It should work.