Skip to content

Instantly share code, notes, and snippets.

@denvit
Last active May 27, 2020 06:28
Show Gist options
  • Save denvit/691a4853353e69becd50d4392586c6d2 to your computer and use it in GitHub Desktop.
Save denvit/691a4853353e69becd50d4392586c6d2 to your computer and use it in GitHub Desktop.
Add custom block on product details page

Add custom block in product page in Magento2

Compatibility: Magento 2.3.5.p2 (I haven't tested on earlier versions)

This gist shows how to show a custom block in the product view page in Magento2.

The files included in this gist are:

  • /app/design/frontend/YOURTHEME/PACKAGE/Magento_Catalog/layout/catalog_product_view.xml
  • /app/design/frontend/YOURTHEME/PACKAGE/Magento_Catalog/templates/product/view/myblock.phtml

NOTE: You have to add this line in your catalog_product_view.xml file:

<referenceBlock name="product.info.social">
    <block class="Magento\Catalog\Block\Product\View" name="product.info.brand" as="brand" template="Magento_Catalog::product/view/brand.phtml"></block>
</referenceBlock>

This will add our custom block at the end of product.info.social block. You may change the ref if you want the block in other positions (e.g.: product.info.details)

<?xml version="1.0"?>
<page 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.social">
<block class="Magento\Catalog\Block\Product\View" name="product.info.brand" as="brand" template="Magento_Catalog::product/view/brand.phtml"></block>
</referenceBlock>
...
</body>
</page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment