1

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>

1 Answer 1

3
+100

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.

8
  • This is definitely the issue. Do you have any idea where to start looking for what might cause one product to not load "MyBlock" while every other product page in the same category is properly loading "MyBlock". This is loading in attributes.phtml for every single other product, shouldn't it do the same for this new one? Commented Mar 16, 2020 at 17:12
  • How is "myBlock" added in the page? What is the name of the xml file where this is added and how does the block configuration look like?
    – Marius
    Commented Mar 16, 2020 at 18:01
  • I think I've added what you asked for as an edit above @Marius. Commented Mar 16, 2020 at 18:44
  • I see that in your xml there is this as="myblock" but you call it in the template as MyBlock. Make sure the values match. They are case sensitive.
    – Marius
    Commented Mar 17, 2020 at 6:15
  • I apologize, I was trying to be extra careful when translating the case. I missed the ones in my template, They are actually lowercase as in my XML. I think the part that's really confusing me is that this exact code is working on 99.9% of the products. It's just this one new product that has an issue. If it were just a broken call, wouldn't it be broken on every product page? Commented Mar 17, 2020 at 13:13

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.