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?
-
what is your static block id?Rakesh Jesadiya– Rakesh Jesadiya2017年05月30日 08:52:09 +00:00Commented May 30, 2017 at 8:52
-
It is 'hello-world' and I've updated the above, as such.YorkieMagento– YorkieMagento2017年05月30日 12:10:45 +00:00Commented May 30, 2017 at 12:10
-
your phtml file is call in tab?Rakesh Jesadiya– Rakesh Jesadiya2017年05月30日 12:11:59 +00:00Commented 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...YorkieMagento– YorkieMagento2017年05月30日 12:15:59 +00:00Commented May 30, 2017 at 12:15
-
Have you check with static text in your phtml file and its called or not?Rakesh Jesadiya– Rakesh Jesadiya2017年05月30日 12:18:39 +00:00Commented May 30, 2017 at 12:18
4 Answers 4
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>
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/
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();
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>
-
Sadly not Baber, the block is already placed within that container.YorkieMagento– YorkieMagento2017年06月03日 15:29:10 +00:00Commented 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-magento2Baber– Baber2017年06月03日 15:36:29 +00:00Commented Jun 3, 2017 at 15:36