At list.phtml, I am trying to get product object or product id for child template, which it comes from other module. How I can get product id at that phtml file?
In magento 1.x it was possible via following technique. when used it into magento 2.x is throwing error
Uncaught Error: Call to a member function setData()
my custom module layout
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="category.products.list">
<action method="setTemplate">
<argument name="template" xsi:type="string">PR_Catalog::product/list.phtml</argument>
</action>
<block class="PR\Catalog\Block\Product\Offers" name="category.products.offers" as="offer_list" template="PR_Catalog::product/offers.phtml" />
</referenceBlock>
</body>
</page>
custom module list.phtml file
<?php foreach ($_productCollection as $_product){ ?>
<?php
$block->getChildBlock("offer_list")->setData("product", $_product);
echo $block->getChildHtml('offer_list')
?>
<?php } ?>
block class code is
<?php
namespace PR\Catalog\Block\Product;
class Offers extends \Magento\Framework\View\Element\Template
{
private $product;
public function setProduct($product)
{
$this->product = $product;
}
public function getProduct()
{
var_dump($this->product);die;
return $this->product;
}
}
child block phtml file
<div class="other-offers">
<?php echo $block->getProduct()->getId(); ?>
</div>