Magento 2.3.4 custom module - Uncaught Error: Call to a member function setData() on boolean

magento.stackexchange https://magento.stackexchange.com/questions/307180

  •  12-04-2021
  •  | 
  •  

Question

I've got a custom Magento 2 module that is causing issues on newly created products. All of the existing ones seem to be okay. I'm running Magento 2.3.4

in my template: /app/design/frontend/MySite/MyTheme/Magento_Catalog/templates/product/view/attributes.phtml

<?php
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $_product = $block->getProduct()
?>

$block->getChildBlock("myblock")->setData("my_data", $_product);
echo $block->getChildHtml('myblock', false); 

The line that reads $block->getChildBlock("MyBlock")->setData("my_data", $_product); is throwing the following error:

PHP Fatal error: Uncaught Error: Call to a member function setData() on boolean in /var/www/mysite.com/app/design/frontend/MySite/MyTheme/Magento_Catalog/templates/product/view/attributes.phtml

Any idea why this would work for all products but newly added ones? The contents of the block being referenced are irrelevant at this point as it's not making it far enough to load it.

Edited to add XML (catalog_product_view.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>
        <move element="product.attributes" destination="product.info.price" after="-"/>
        <referenceBlock name="product.attributes">
            <block class="Catalog\MyBlock\Block\Catalog\Product\Myblock" name="catalogmyblock" as="myblock" template="Catalog_MyBlock::view/myblock.phtml">
            </block>
        </referenceBlock>
        <!-- <referenceContainer name="product.info.price" remove="true"/> -->
        <referenceContainer name="product.info.addtocart" remove="true"/>

    </body>
</page>
Was it helpful?

Solution

The error means that the code $block->getChildBlock("MyBlock") returns false.
There is no block called MyBlock in your layout.

The problem can have multiple causes: - you did not add a block that has as="MyBlock".
- you did not add a block that has as="MyBlock" as a child block of your current block. - the block is removed from another layout file or a plugin or something else that gets executed before the layout is rendered.

I think you can check all the child names of your current block like this:

 $block->getLayout()->getChildnames($block->getNameInLayout());

if your block is not in there, try making sure it is added properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top