Hi, I have created a custom tab with the following code in catalog_product_view.xml
<block class="Magento\Catalog\Block\Product\View" name="ingredients.tab" as="ingredients" template="Magento_Theme::html/ingredients_tab.phtml" group="detailed_info" > <arguments> <argument translate="true" name="title" xsi:type="string">Ingredients & Nutrition</argument> <argument name="at_call" xsi:type="string">getNutritional</argument> <argument name="at_code" xsi:type="string">nutritional</argument> <argument name="css_class" xsi:type="string">nutritional</argument> <argument name="at_label" xsi:type="string">Nutritional</argument> <argument name="add_attribute" xsi:type="string">itemprop="nutritional"</argument> <argument name="sort_order" xsi:type="string">40</argument> </arguments> </block>
This sends across the text value of my custom attribute "nutritional" great. The problem is, I need to also get the value of another custom attribute, 'ingredients'. How can I send across two attributes?
The corresponding file ingredients_tab.phtml reads:
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
if ($_attributeType && $_attributeType == 'text') {
    $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code)) ? $_product->getAttributeText($_code) : '';
} else {
    $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>
<?php if ($_attributeValue): ?>
<div class="product attribute <?php /* @escapeNotVerified */ echo $_className?>">
    <?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php /* @escapeNotVerified */ echo $_attributeLabel?></strong><?php endif; ?>
    <div class="value" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?>><?php /* @escapeNotVerified */ echo $_attributeValue; ?></div>
</div>
<?php endif; ?>How can I extend this so it gets the value of multiple custom attributes?
Thank you.
Hello @christopher_oliver ,
You can use child block and pass arguments in that child blocks and then you can render the child block in your in parent block phtml. As
<block class="Magento\Catalog\Block\Product\View" name="ingredients.tab" as="ingredients" template="Magento_Theme::html/ingredients_tab.phtml" group="detailed_info" >
	<block class="Magento\Catalog\Block\Product\View" name="product.info.ingredients" as="ingredients" template="Magento_Theme::product/view/ingredients.phtml">
              <arguments>
			<argument translate="true" name="title" xsi:type="string">Ingredients & Nutrition</argument>
			<argument name="at_call" xsi:type="string">getNutritional</argument>
			<argument name="at_code" xsi:type="string">nutritional</argument>
			<argument name="css_class" xsi:type="string">nutritional</argument>
			<argument name="at_label" xsi:type="string">Nutritional</argument>
			<argument name="add_attribute" xsi:type="string">itemprop="nutritional"</argument>
			<argument name="sort_order" xsi:type="string">40</argument>
		</arguments>
        </block>
        <block class="Magento\Catalog\Block\Product\View" name="product.nutritional" as="nutritional" template="Magento_Catalog::product/view/nutritional.phtml">
    		<arguments>
                        <argument translate="true" name="title" xsi:type="string">More Information</argument>
                        <argument name="sort_order" xsi:type="string">20</argument>
                </arguments>
        </block>
</block>In parent ingredients_tab.phtml, you can get child block data as:
$block->getChildData('ingredients','title');
$block->getChildData('nutritional','title');
Here $block->getChildData($alias, $dataname);Hope this will help you. If still you get any issue, please let me know.
If it helps you, please accept it as solution and give kudos.
Regards
Hi @Sarvagya Pandey, thank you for the reply.
I have tried the code suggested. My catalog_product_view.xml:
<block class="Magento\Catalog\Block\Product\View" name="ingredients.tab" as="ingredients" template="Magento_Theme::html/ingredients_tab.phtml" group="detailed_info" >
	<block class="Magento\Catalog\Block\Product\View" name="product.info.ingredients" as="ingredients" template="Magento_Catalog::product/view/ingredients.phtml">
    <arguments>
			<argument translate="true" name="title" xsi:type="string">Ingredients & Nutrition</argument>
			<argument name="at_call" xsi:type="string">getNutritional</argument>
			<argument name="at_code" xsi:type="string">nutritional</argument>
			<argument name="css_class" xsi:type="string">nutritional</argument>
			<argument name="at_label" xsi:type="string">Nutritional</argument>
			<argument name="add_attribute" xsi:type="string">itemprop="nutritional"</argument>
			<argument name="sort_order" xsi:type="string">40</argument>
		</arguments>
   </block>
   <block class="Magento\Catalog\Block\Product\View" name="product.nutritional" as="nutritional" template="Magento_Catalog::product/view/nutritional.phtml">
    	<arguments>
        <argument translate="true" name="title" xsi:type="string">More Information</argument>
        <argument name="sort_order" xsi:type="string">20</argument>
      </arguments>
   </block>
</block>In ingredients_tab.phtml:
<?php
$block->getChildData('ingredients','title');
$block->getChildData('nutritional','title');
$block->getChildData($alias, $dataname);
?>What should be in ingredients.phtml and nutritional.phtml? They are currently blank.
The error I am receiving is:
Exception #0 (Exception): Notice: Undefined variable: alias in /home/newcake/public_html/app/design/frontend/Dac-theme/theme/Magento_Theme/templates/html/ingredients_tab.phtml on line 5
Thank you,
Chris
Don't use
$block->getChildData($alias, $dataname);
in your phtml.
I have provided this just to understand that in
$block->getChildData($alias, $dataname);
function $alias is the alias on block given in layout (using as="....") and
$dataname is the variable which you want to access.
Hope this will work for you.
Regards.
Thank you for clarifying this.
I have the code
<?php
$block->getChildData('ingredients','title');
$block->getChildData('nutritional','title');
?>But nothing shows. What is the 'title' referring to? I just want the title of the attribute and the value of it's textarea but nothing shows.
Thank you
Hello @christopher_oliver ,
Please refer the following magento core code so that you can understand how magento use these functionality:
I hope this will help you. If still you face any issue please let me know.
If it helps you please accept it as solution and give us kudos.
Regards.