Passing variables to a child html block in Magento 2Pass data to getChildHtml() in foreach loop - Magento 2Magento + JQuery + Ajax - How do I reload just parts of my custom Module instead of the whole Block?getLayout()->createBlock()->setdata() not passing variablesmagento 2 captcha not rendering if I override layout xmlWhy Getting categories and names on product view page Magento 2 fails?Magento 2 add custom attribute to container in layout.xmlMagento2 Override Block - Invalid template file problemPassing variables to JS CompponentProgramatically get html for block with child blocksMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3.0 - Create Product Page H1 title with product name and attribute name
What should you do if you miss a job interview (deliberately)?
How to explain what's wrong with this application of the chain rule?
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
In Qur'an 7:161, why is "say the word of humility" translated in various ways?
Store Credit Card Information in Password Manager?
Drawing ramified coverings with tikz
Is it possible to have a strip of cold climate in the middle of a planet?
Has any country ever had 2 former presidents in jail simultaneously?
When a Cleric spontaneously casts a Cure Light Wounds spell, will a Pearl of Power recover the original spell or Cure Light Wounds?
What prevents the use of a multi-segment ILS for non-straight approaches?
Longest common substring in linear time
Why is so much work done on numerical verification of the Riemann Hypothesis?
Can I sign legal documents with a smiley face?
Is it better practice to read straight from sheet music rather than memorize it?
It grows, but water kills it
Not using 's' for he/she/it
Lowest total scrabble score
Fear of getting stuck on one programming language / technology that is not used in my country
Is it safe to use olive oil to clean the ear wax?
Calculating Wattage for Resistor in High Frequency Application?
Is this toilet slogan correct usage of the English language?
Why do compilers behave differently when static_cast(ing) a function to void*?
250 Floor Tower
What was this official D&D 3.5e Lovecraft-flavored rulebook?
Passing variables to a child html block in Magento 2
Pass data to getChildHtml() in foreach loop - Magento 2Magento + JQuery + Ajax - How do I reload just parts of my custom Module instead of the whole Block?getLayout()->createBlock()->setdata() not passing variablesmagento 2 captcha not rendering if I override layout xmlWhy Getting categories and names on product view page Magento 2 fails?Magento 2 add custom attribute to container in layout.xmlMagento2 Override Block - Invalid template file problemPassing variables to JS CompponentProgramatically get html for block with child blocksMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3.0 - Create Product Page H1 title with product name and attribute name
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="PRCatalogBlockProductOffers" 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 PRCatalogBlockProduct;
class Offers extends MagentoFrameworkViewElementTemplate
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>
magento2 blocks
add a comment |
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="PRCatalogBlockProductOffers" 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 PRCatalogBlockProduct;
class Offers extends MagentoFrameworkViewElementTemplate
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>
magento2 blocks
add a comment |
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="PRCatalogBlockProductOffers" 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 PRCatalogBlockProduct;
class Offers extends MagentoFrameworkViewElementTemplate
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>
magento2 blocks
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="PRCatalogBlockProductOffers" 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 PRCatalogBlockProduct;
class Offers extends MagentoFrameworkViewElementTemplate
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>
magento2 blocks
magento2 blocks
edited Mar 21 '18 at 8:53
Mujahidh
1,41012036
1,41012036
asked Apr 25 '16 at 10:02
Praful RajputPraful Rajput
2,84472146
2,84472146
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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')
.
still not working...
– Praful Rajput
Apr 25 '16 at 10:33
Can you post your current code under your question?
– Ideo
Apr 25 '16 at 10:35
I have posted it.
– Praful Rajput
Apr 25 '16 at 10:43
Check my edited answer
– Ideo
Apr 25 '16 at 10:47
1
we have to use$block->getChildHtml('block_name', false);
i.e. set cache disabled.
– Praful Rajput
Apr 26 '16 at 12:31
|
show 1 more comment
Use below code:
$block->getChildBlock("offer_list")->setData("product", $_product);
to
$this->getLayout()->createBlock('PRCatalogBlockProductOffers')->setProduct($_product);
add a comment |
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(); ?>
add a comment |
First, we need to call a phtml file from the block using the below code.
From Static Block:
block class="VendorExtesnionBlockCustomblock" name="customblock" template="Vendor_Extension::filename.phtml" variableName="123"
From another PHTML file:
echo $this->getLayout()->createBlock("VendorExtesnionBlockCustomblock")->setTemplate("Vendor_Extension::filename.phtml")->setData("variableName", 123)->toHtml();
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.
New contributor
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f112465%2fpassing-variables-to-a-child-html-block-in-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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')
.
still not working...
– Praful Rajput
Apr 25 '16 at 10:33
Can you post your current code under your question?
– Ideo
Apr 25 '16 at 10:35
I have posted it.
– Praful Rajput
Apr 25 '16 at 10:43
Check my edited answer
– Ideo
Apr 25 '16 at 10:47
1
we have to use$block->getChildHtml('block_name', false);
i.e. set cache disabled.
– Praful Rajput
Apr 26 '16 at 12:31
|
show 1 more comment
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')
.
still not working...
– Praful Rajput
Apr 25 '16 at 10:33
Can you post your current code under your question?
– Ideo
Apr 25 '16 at 10:35
I have posted it.
– Praful Rajput
Apr 25 '16 at 10:43
Check my edited answer
– Ideo
Apr 25 '16 at 10:47
1
we have to use$block->getChildHtml('block_name', false);
i.e. set cache disabled.
– Praful Rajput
Apr 26 '16 at 12:31
|
show 1 more comment
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')
.
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')
.
edited Apr 25 '16 at 11:59
Praful Rajput
2,84472146
2,84472146
answered Apr 25 '16 at 10:05
IdeoIdeo
1,732718
1,732718
still not working...
– Praful Rajput
Apr 25 '16 at 10:33
Can you post your current code under your question?
– Ideo
Apr 25 '16 at 10:35
I have posted it.
– Praful Rajput
Apr 25 '16 at 10:43
Check my edited answer
– Ideo
Apr 25 '16 at 10:47
1
we have to use$block->getChildHtml('block_name', false);
i.e. set cache disabled.
– Praful Rajput
Apr 26 '16 at 12:31
|
show 1 more comment
still not working...
– Praful Rajput
Apr 25 '16 at 10:33
Can you post your current code under your question?
– Ideo
Apr 25 '16 at 10:35
I have posted it.
– Praful Rajput
Apr 25 '16 at 10:43
Check my edited answer
– Ideo
Apr 25 '16 at 10:47
1
we have to use$block->getChildHtml('block_name', false);
i.e. set cache disabled.
– Praful Rajput
Apr 26 '16 at 12:31
still not working...
– Praful Rajput
Apr 25 '16 at 10:33
still not working...
– Praful Rajput
Apr 25 '16 at 10:33
Can you post your current code under your question?
– Ideo
Apr 25 '16 at 10:35
Can you post your current code under your question?
– Ideo
Apr 25 '16 at 10:35
I have posted it.
– Praful Rajput
Apr 25 '16 at 10:43
I have posted it.
– Praful Rajput
Apr 25 '16 at 10:43
Check my edited answer
– Ideo
Apr 25 '16 at 10:47
Check my edited answer
– Ideo
Apr 25 '16 at 10:47
1
1
we have to use
$block->getChildHtml('block_name', false);
i.e. set cache disabled.– Praful Rajput
Apr 26 '16 at 12:31
we have to use
$block->getChildHtml('block_name', false);
i.e. set cache disabled.– Praful Rajput
Apr 26 '16 at 12:31
|
show 1 more comment
Use below code:
$block->getChildBlock("offer_list")->setData("product", $_product);
to
$this->getLayout()->createBlock('PRCatalogBlockProductOffers')->setProduct($_product);
add a comment |
Use below code:
$block->getChildBlock("offer_list")->setData("product", $_product);
to
$this->getLayout()->createBlock('PRCatalogBlockProductOffers')->setProduct($_product);
add a comment |
Use below code:
$block->getChildBlock("offer_list")->setData("product", $_product);
to
$this->getLayout()->createBlock('PRCatalogBlockProductOffers')->setProduct($_product);
Use below code:
$block->getChildBlock("offer_list")->setData("product", $_product);
to
$this->getLayout()->createBlock('PRCatalogBlockProductOffers')->setProduct($_product);
answered Apr 25 '16 at 10:48
Prashant ValandaPrashant Valanda
9,84212355
9,84212355
add a comment |
add a comment |
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(); ?>
add a comment |
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(); ?>
add a comment |
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(); ?>
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(); ?>
answered Feb 21 '17 at 16:12
MagentoManMagentoMan
15819
15819
add a comment |
add a comment |
First, we need to call a phtml file from the block using the below code.
From Static Block:
block class="VendorExtesnionBlockCustomblock" name="customblock" template="Vendor_Extension::filename.phtml" variableName="123"
From another PHTML file:
echo $this->getLayout()->createBlock("VendorExtesnionBlockCustomblock")->setTemplate("Vendor_Extension::filename.phtml")->setData("variableName", 123)->toHtml();
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.
New contributor
add a comment |
First, we need to call a phtml file from the block using the below code.
From Static Block:
block class="VendorExtesnionBlockCustomblock" name="customblock" template="Vendor_Extension::filename.phtml" variableName="123"
From another PHTML file:
echo $this->getLayout()->createBlock("VendorExtesnionBlockCustomblock")->setTemplate("Vendor_Extension::filename.phtml")->setData("variableName", 123)->toHtml();
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.
New contributor
add a comment |
First, we need to call a phtml file from the block using the below code.
From Static Block:
block class="VendorExtesnionBlockCustomblock" name="customblock" template="Vendor_Extension::filename.phtml" variableName="123"
From another PHTML file:
echo $this->getLayout()->createBlock("VendorExtesnionBlockCustomblock")->setTemplate("Vendor_Extension::filename.phtml")->setData("variableName", 123)->toHtml();
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.
New contributor
First, we need to call a phtml file from the block using the below code.
From Static Block:
block class="VendorExtesnionBlockCustomblock" name="customblock" template="Vendor_Extension::filename.phtml" variableName="123"
From another PHTML file:
echo $this->getLayout()->createBlock("VendorExtesnionBlockCustomblock")->setTemplate("Vendor_Extension::filename.phtml")->setData("variableName", 123)->toHtml();
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.
New contributor
New contributor
answered yesterday
Ajay PatidarAjay Patidar
11
11
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f112465%2fpassing-variables-to-a-child-html-block-in-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown