Magento 2 – Add CMS Block as Custom Tab in Product View Page

magento2product-viewtabs

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?

Best Answer

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>
Related Topic