5

I'd like to add a static block as an additional custom tab in Magento 2.

CMS Static Block (hello-world)

<p>Hello world!</p>

catalog_product_view.xml

<block class="Magento\Catalog\Block\Product\View" name="product.helloworld" template="product/view/hello-world.phtml" group="detailed_info" >
 <arguments>
 <argument translate="true" name="title" xsi:type="string">Hello World</argument>
 <argument name="priority" xsi:type="string">4</argument>
 </arguments>
</block>

hello-world.phtml

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('hello-world')->toHtml();?>

At the moment nothing is being displayed. Can anyone suggest an edit to get the code above working?

asked May 29, 2017 at 14:39
9
  • what is your static block id? Commented May 30, 2017 at 8:52
  • It is 'hello-world' and I've updated the above, as such. Commented May 30, 2017 at 12:10
  • your phtml file is call in tab? Commented May 30, 2017 at 12:11
  • That's what I am trying to achieve but at the moment with the code above, the tab doesn't appear... Commented May 30, 2017 at 12:15
  • Have you check with static text in your phtml file and its called or not? Commented May 30, 2017 at 12:18

4 Answers 4

4
+50

Add Vendor_Module before template path

Try this, catalog_product_view.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceBlock name="product.info.details">
 <block class="Magento\Catalog\Block\Product\View" name="product.helloworld" template="Vendor_Module::product/view/hello-world.phtml" group="detailed_info">
 <arguments>
 <argument translate="true" name="title" xsi:type="string">Hello World</argument>
 </arguments>
 </block>
 </referenceBlock>
 </body>
</page>
answered Jun 4, 2017 at 18:38
2

Change your catalog_product_view.xml by


<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceBlock name="product.info.details">
 <block class="Magento\Catalog\Block\Product\View" name="product.helloworld" template="Vendor_Module::product/view/hello-world.phtml" group="detailed_info">
 <arguments>
 <argument translate="true" name="title" xsi:type="string">Hello World</argument>
 <argument name="priority" xsi:type="string">4</argument>
 </arguments>
 </block>
 </referenceBlock>
 </body>
</page>

Your template attribute should be Vendor_Module::product/view/hello-world.phtml

Flush cache OR delete var/cache/ and var/page_cache/

answered Jun 3, 2017 at 16:11
2

You can reference the static block directly within xml and avoid having to use the phtml file using below. This is tested and working on my store:

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceBlock name="product.info.details">
 <block class="Magento\Cms\Block\Block" name="product.helloworld.tab" as="helloworld.tab" group="detailed_info" >
 <arguments>
 <argument translate="true" name="title" xsi:type="string">Hello World</argument>
 <argument name="block_id" xsi:type="string">hello-world</argument>
 </arguments>
 </block>
 </referenceBlock>
 </body>
 </page>

Above XML should go in your theme within:

'Magento_Catalog/layout/catalog_product_view.xml'

Or if within a module:

'view/frontend/layout/catalog_product_view.xml'

Then make sure cache is flushed.

bin/magento cache:clean layout

I normally use the below however to reference a static block within phtml if you need to continue using the template method maybe try this within the template phtml file:

echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('hello-world')->toHtml();
answered Jun 5, 2017 at 7:27
0

I think you are missing the reference to the product tabs container block, try the below:

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceBlock name="product.info.details">
<block class="Magento\Catalog\Block\Product\View" name="product.helloworld" template="product/view/hello-world.phtml" group="detailed_info" >
 <arguments>
 <argument translate="true" name="title" xsi:type="string">Hello World</argument>
 <argument name="priority" xsi:type="string">4</argument>
 </arguments>
</block>
</referenceBlock>
 </body>
</page>
answered Jun 3, 2017 at 15:26
2
  • Sadly not Baber, the block is already placed within that container. Commented Jun 3, 2017 at 15:29
  • :(, I would have a look at this guide it helped me create the custom tabs I needed - blog.landofcoder.com/add-custom-tab-product-page-magento2 Commented Jun 3, 2017 at 15:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.