Can anyone explain how works getChild() method because I have some lines of code that use this method and I don't know exactly what it does. This is the code:

if ($this->getChild('name.after')) {
    $_nameAfterChildren = $this->getChild('name.after')->getSortedChildren();
    foreach ($_nameAfterChildren as $_nameAfterChildName) {
        $_nameAfterChild = $this->getChild('name.after')->getChild($_nameAfterChildName);
        $_products[$_product->getId()]['name_after'] .= $_nameAfterChild->toHtml();

I have this code in two different parts, one is on product listing page and one on a block that I want to display on homepage. The problem is that on the product listing page works perfectly and the code is executed ($this->getChild('name.after') this returns true) but on the other part returns false and it doesn't execute the code inside the if statement.

Can anyone can tell me how should my xml files looks like?

1 Answer 1


Magento's MVC paradigm is implemented a bit differently than the classical one.
The V(iew) is actually a set of blocks that are classes with logic rendered by a template.
Each block has a name.
The blocks support child blocks with their own logic.
The method getChild supported by each block retrieves one of the child blocks from the current block with the name you specify as parameter.

For example if you have this in your layout files.

<block type="some/class_here" name="parent.block" ...>
    <block type="other/class_name" name="child.block.one" ..>
        <block type="more/block_here" name="child.block.one.one" ..>
    <block type="something/class_name" name="child.block.two" ..>

Inside the block with the name parent.block if you call $this->getChild('child.block.one') will get you the instance of the child block with the name child.block.one.
Calling $this->getChild('child.block.two') will get you the instance of the child block with the name child.block.two.

Calling $this->getChild('something.that.does.not.exist') will get you null.

Calling $this->getChild('child.block.one.one') inside the block child.block.one will get you the instance of the child block with the name child.block.one.one.

Calling $this->getChild('child.block.one.one') inside the block parent.block will get you null since child.block.one.one is not a child of the parent.block but a "grand-child" of it.

  • Thank you. This answer is so detailed. And can you tell me please in which xml file do I have to put a block for showing it in homepage on my custom theme?
    – Ciprian
    Commented Feb 2, 2018 at 11:16
  • You can put it in any xml. Just make sure the layout handle (main xml tag) is cms_index_index to make it appear only on homepage.
    – Marius
    Commented Feb 2, 2018 at 12:38

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.