2

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();
?>
5
  • Did you tried My solution ?
    – Pawan
    Commented Aug 20, 2018 at 17:35
  • @pawan yeah but it won't work
    – Nismath vi
    Commented Aug 23, 2018 at 6:36
  • what issue you are getting ? Because I checked it and it working fine.
    – Pawan
    Commented Sep 6, 2018 at 3:14
  • Good to know that, Welcome :)
    – Pawan
    Commented Sep 6, 2018 at 4:19
  • @Nismathvi did you manage to get it working? Commented Jan 17, 2020 at 16:20

3 Answers 3

4

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!

1
  • How does this work? is this for all product page? Commented Jan 17, 2020 at 14:39
1

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
0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.