Passing variables to a child html block in Magento 2
You have to define public function setProduct
in your block class:
public function setProduct($product)
{
$this->product = $product;
}
and then you will be able to get it in your block :)
Also, use getChildBlock
function instead of getChild
Now you have to use your newly created function, setProduct($_product)
instead of setData('product', $_product')
.
Use below code:
$block->getChildBlock("offer_list")->setData("product", $_product);
to
$this->getLayout()->createBlock('PR\Catalog\Block\Product\Offers')->setProduct($_product);
In your block class add the following:
protected $product;
public function setProduct($_product){
$this->product = $_product;
}
public function getProduct(){
return $this->product;
}
In the template file that you call the child block do it like this:
<?php /* Start Custom Changes */ ?>
<?php if ($myBlock = $block->getChildBlock('my_child_block')): ?>
<?php
$myBlock->setProduct($_product);
echo $block->getChildHtml('my_child_block', false);
?>
<?php endif; ?>
<?php /* End Custom Changes */ ?>
Now in your child block template file:
<?php echo $block->getProduct()->getId(); ?>