8

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>
8

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').

| improve this answer | |
  • still not working... – Praful Rajput Apr 25 '16 at 10:33
  • Can you post your current code under your question? – Bartłomiej Szubert Apr 25 '16 at 10:35
  • I have posted it. – Praful Rajput Apr 25 '16 at 10:43
  • Check my edited answer – Bartłomiej Szubert Apr 25 '16 at 10:47
  • 2
    we have to use $block->getChildHtml('block_name', false); i.e. set cache disabled. – Praful Rajput Apr 26 '16 at 12:31
3

Use below code:

$block->getChildBlock("offer_list")->setData("product", $_product);

to

$this->getLayout()->createBlock('PR\Catalog\Block\Product\Offers')->setProduct($_product);
| improve this answer | |
3

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(); ?>
| improve this answer | |
2

First, we need to call a phtml file from the block using the below code.

From Static Block:

{{block class="Vendor\Extesnion\Block\Customblock" name="customblock" template="Vendor_Extension::filename.phtml" variableName="123"}}

From another PHTML file:

echo $this->getLayout()->createBlock("Vendor\Extesnion\Block\Customblock")->setTemplate("Vendor_Extension::filename.phtml")->setData("variableName", 123)->toHtml();

From XML

<block class="Vendor\Extesnion\Block\Customblock" name="block_unique_name" after="-" template="Vendor_Extension::filename.phtml"> 
    <arguments>
       <argument name="variableName" translate="true" xsi:type="string">123</argument>
   </arguments>
</block>

In the above code, We have passed the value of variableName

Now we need to get that additional data on phtml file like this.

$variablevalue = $block->getData('variableName');

OR

$variablevalue = $block->getVariableName();

That's it. Simply clear cache.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.