Question

How to add a custom block in product view page after product info. I have created a custom block via admin and its id is category-block-1 I have a custom them and i tried override catalog_product_view.xml.

Magento_Catalog/layout/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.block" template="Magento_Catalog::category-block.phtml" >
      </block>
    </referenceBlock>
</body>
</page>

Magento_Catalog/template/category-block.phtml

<?php
  echo $this->getLayout()
          ->createBlock('Magento\Cms\Block\Block')
          ->setBlockId('category-block-1')
          ->toHtml();
?>
Was it helpful?

Solution

Why you need phtml, You can directly call static block. You can use below code in:

[VENDOR_NAME]/[THEME_NAME]/Magento_Catalog/layout/catalog_product_view.xml

<referenceContainer name="product.info.main">
    <block class="Magento\Cms\Block\Block" name="myPromo" after="-">
        <arguments>
            <argument name="block_id" xsi:type="string">category-block-1</argument>
        </arguments>
    </block>
</referenceContainer>

Hope above will Help!

OTHER TIPS

You don't need to call Magento\Catalog\Block\Product\View in layout file better way to call any template file is to call Magento\Framework\View\Element\Template

Another way is to call the static block directly in below file:

app/design/frontend/Your_vendor/your_theme_name/Magento_Catalog/templates/product/view/details.phtml

Only the block named "content" has an "auto render" feature so you want to call <?php echo $block->getChildHtml('product.block'); ?> inside /[VENDOR_NAME]/[THEME_NAME]/Magento_Catalog/templates/product/view/details.phtml

PS: You probably don't need to load the class="Magento\Catalog\Block\Product\View" again so better to use Magento\Framework\View\Element\Template

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top