Magento 2.2.4 – Not Able to Add Custom phtml File on Product Page

magento2magento2.2magento2.2.4product

I can't able to add/include my custom phtml file on product detailed page.

Here is my entire flow :

  1. Added the following code in app/design/frontend/Themename/themedirectory/Magento_Catalog/layout/catalog_product_view.xml file

    <body>
            <referenceContainer name="content">
                <container name="product-main-content" htmlTag="div" htmlClass="product-main-content" before="-">
                    </container>
                   </referenceContainer>
    
            <referenceContainer name="content.top.most">
                <container name="custom.block.sec" htmlTag="div" htmlClass="custom-class" before="-">
                <block class="Magento\Catalog\Block\Product\View" name="my_custom" template="Vendorname_Modulename::custom.phtml" />
                </container>
            </referenceContainer>
    
              <block class="Magento\Theme\Block\Html\Title" name="page.main.sub.title" template="Magento_Theme::html/title.phtml"/>
    
                <referenceBlock name="product.info.details">
              <block class="Magento\Catalog\Block\Product\View" name="cmsinfo.tab" as="cmsinfo" template="product/view/cms_tab.phtml" group="detailed_info" >
                <arguments>
                  <argument translate="true" name="title" xsi:type="string">Custom Tab</argument>
                </arguments>
               </block>
             </referenceBlock>
    
              <move element="custom.block.sec" destination="product.info.main" before="-"/>
    
    </body>
    
  2. Added the custom.phtml file in app/code/Vendorname/Modulename/view/frontend/templates/custom.phtml

  3. custom.phtml file content

    Custom File Works

I referred this link as well but no luck.

But its not working. Why?

Can anyone let me know what's the issue & how to add the custom file in product page?

Best Answer

Please try this way in your app/design/frontend/Themename/themedirectory/Magento_Catalog/layout/catalog_product_view.xml file

<referenceContainer name="content.top.most">
    <container name="custom.block.sec" htmlTag="div" htmlClass="custom-class" before="-">
        <block class="Magento\Catalog\Block\Product\View" name="my_custom" template="Vendorname_Modulename::custom.phtml" />
    </container>
</referenceContainer>

<move element="custom.block.sec" destination="product.info.main" before="-"/>

After that please check with clear your cache.