Magento2 – How to Add Custom Block to Products Page

attributesmagento2product

I'm looking to add custom product attributes to the product page. Basically adding a block under the title to list product attributes.

I looked at echoing out the attributes in

Magento_Catalog/templates/product/view/attributes.phtml

file but that seemed messy. So I was hoping there'd be a way to add a custom block to the xml and then rendering my own .phtml file? That way I'm keeping the core code seperate from my own (obviously I'm adding all this into my own theme files and not touching the vendor files.)

Would this be possible and/or the best way?

Best Answer

In app\code\Namespace\Modulename\view\frontend\layout\catalog_product_view.xml add like below code. You can use your required referenceContainer

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="alert.urls">
            <block class="Namespace\Modulename\Block\Catalog\Product\View\Module" name="textblock" as="textblock" after="-" template="catalog/product/view/textblock.phtml" />
        </referenceContainer>
    </body>
</page>
Related Topic