Magento2 – Create Block on Product Page via XML

magento2product

I have created a phtml file which I want to add to my product pages.

I know that there are two containers at the top which are product.info.media and product.info.main. Then there is product.info.details, the area where the tabs are and the description starts (product.info.description).

My problem: I can't reference the exact spot where I want to place my block. It is supposed to be displayed just between the two div classes product media and product info detailed.

I have tried

<referenceContainer name="product.info.media">
    <container name="product.info.afterprice" after="-" label="Product info afterprice" htmlTag="div" htmlClass="product-info-after-price">
        <block class="Magento\Framework\View\Element\Template" name="product-extra"  template="Magento_Theme::html/added/product-extra.phtml" />
    </container>
</referenceContainer>

OR

<move element="product.info.afterprice" destination="product.info.details" before="-" />

but never shows where I want it to be. How can I reference the main product content area and say after="product.info.media" or before="product.info.details"?

Best Answer

Add below code to app/design/frontend/{{vendor}}/{{companyname}}/Magento_Catalog/layout/catalog_product_view.xml file

<referenceContainer name="content">
    <block class="Magento\Framework\View\Element\Template" after="product.info.media" name="product-extra" template="Magento_Theme::html/added/product-extra.phtml"/>
</referenceContainer>

If you will add my solution on your catalog_product_view.xml file then you will not required to move it.

Related Topic