Magento2 : How to add custom block on product page












1














I want to add my custom template on product page just like $this->getChildHtml('custom_product_info').



As in magento1.9 to use $this->getChildHtml('custom_product_info') we add layout xml like below :



<catalog_product_view>
<reference name="product.info">
<block type="core/template" name="custom_product_info" template="custom.phtml"/>
</reference>
</catalog_product_view>


What is the xml and process to add custom template in Magento2.1?










share|improve this question



























    1














    I want to add my custom template on product page just like $this->getChildHtml('custom_product_info').



    As in magento1.9 to use $this->getChildHtml('custom_product_info') we add layout xml like below :



    <catalog_product_view>
    <reference name="product.info">
    <block type="core/template" name="custom_product_info" template="custom.phtml"/>
    </reference>
    </catalog_product_view>


    What is the xml and process to add custom template in Magento2.1?










    share|improve this question

























      1












      1








      1







      I want to add my custom template on product page just like $this->getChildHtml('custom_product_info').



      As in magento1.9 to use $this->getChildHtml('custom_product_info') we add layout xml like below :



      <catalog_product_view>
      <reference name="product.info">
      <block type="core/template" name="custom_product_info" template="custom.phtml"/>
      </reference>
      </catalog_product_view>


      What is the xml and process to add custom template in Magento2.1?










      share|improve this question













      I want to add my custom template on product page just like $this->getChildHtml('custom_product_info').



      As in magento1.9 to use $this->getChildHtml('custom_product_info') we add layout xml like below :



      <catalog_product_view>
      <reference name="product.info">
      <block type="core/template" name="custom_product_info" template="custom.phtml"/>
      </reference>
      </catalog_product_view>


      What is the xml and process to add custom template in Magento2.1?







      product magento-2.1.7






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 '18 at 6:15









      Vinaya Maheshwari

      1,16211846




      1,16211846






















          5 Answers
          5






          active

          oldest

          votes


















          2














          Try this:



          <?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">
          <block class="{Vendor}{Module}BlockCatalogProductView{Class}" name="{your.block.name}" template="{Vendor}_{Module}::{template_name}.phtml">
          <!-- If you have any arguments to pass -->
          <arguments>
          <argument translate="true" name="{argument_name}" xsi:type="string">{Your argument}</argument>
          </arguments>
          </block>
          </referenceBlock>
          </body>
          </page>





          share|improve this answer





















          • For class="{Vendor}{Module}BlockCatalogProductView{Class}" should I create a block class, please post a example for block class.
            – Vinaya Maheshwari
            Mar 26 '18 at 6:26










          • You should be able to use this block: MagentoFrameworkViewElementTemplate
            – hungersoft
            Mar 26 '18 at 6:27










          • How can I get $product in custom template file?
            – Vinaya Maheshwari
            Mar 26 '18 at 9:28










          • Follow this tutorial: blog.chapagain.com.np/…
            – hungersoft
            Mar 26 '18 at 9:30



















          1














          If you want to add this custom block via Custom module,
          Then create catalog_product_view.xml at app/code/{Vendorname}/{moduleName}/view/frontend/layout,.



          <?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">
          <block class="{Vendor}{Module}Block{BLOCK_CLASSS}" name="{block.name.inlayout}" as="{name_aliase_path}"
          template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
          </block>
          </referenceBlock>
          </body>
          </page>


          Then you can call your block via at code at form.phtml at



          <?= $block->getChildHtml({name_aliase_path}) ?>





          share|improve this answer























          • Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
            – inrsaurabh
            Mar 26 '18 at 6:59



















          1














          Use this



          <?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">
          <block class="MagentoFrameworkViewElementTemplate" name="block.name" as="block.alias"
          template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
          </block>
          </referenceBlock>
          </body>
          </page>





          share|improve this answer





























            0














             <referenceContainer name="product.info.main">
            <block class="MagentoFrameworkViewElementTemplate" name="{argument_name}" template="template_name.phtml"/>
            </referenceContainer>





            share|improve this answer





























              0














              <referenceContainer name="content">

              <block class="XcommerceCategoryBlockOverview" name="overview"
              template="X2commerce_Category::custom.phtml">
              </block>
              </referenceContainer>





              share|improve this answer





















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "479"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f219813%2fmagento2-how-to-add-custom-block-on-product-page%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                Try this:



                <?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">
                <block class="{Vendor}{Module}BlockCatalogProductView{Class}" name="{your.block.name}" template="{Vendor}_{Module}::{template_name}.phtml">
                <!-- If you have any arguments to pass -->
                <arguments>
                <argument translate="true" name="{argument_name}" xsi:type="string">{Your argument}</argument>
                </arguments>
                </block>
                </referenceBlock>
                </body>
                </page>





                share|improve this answer





















                • For class="{Vendor}{Module}BlockCatalogProductView{Class}" should I create a block class, please post a example for block class.
                  – Vinaya Maheshwari
                  Mar 26 '18 at 6:26










                • You should be able to use this block: MagentoFrameworkViewElementTemplate
                  – hungersoft
                  Mar 26 '18 at 6:27










                • How can I get $product in custom template file?
                  – Vinaya Maheshwari
                  Mar 26 '18 at 9:28










                • Follow this tutorial: blog.chapagain.com.np/…
                  – hungersoft
                  Mar 26 '18 at 9:30
















                2














                Try this:



                <?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">
                <block class="{Vendor}{Module}BlockCatalogProductView{Class}" name="{your.block.name}" template="{Vendor}_{Module}::{template_name}.phtml">
                <!-- If you have any arguments to pass -->
                <arguments>
                <argument translate="true" name="{argument_name}" xsi:type="string">{Your argument}</argument>
                </arguments>
                </block>
                </referenceBlock>
                </body>
                </page>





                share|improve this answer





















                • For class="{Vendor}{Module}BlockCatalogProductView{Class}" should I create a block class, please post a example for block class.
                  – Vinaya Maheshwari
                  Mar 26 '18 at 6:26










                • You should be able to use this block: MagentoFrameworkViewElementTemplate
                  – hungersoft
                  Mar 26 '18 at 6:27










                • How can I get $product in custom template file?
                  – Vinaya Maheshwari
                  Mar 26 '18 at 9:28










                • Follow this tutorial: blog.chapagain.com.np/…
                  – hungersoft
                  Mar 26 '18 at 9:30














                2












                2








                2






                Try this:



                <?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">
                <block class="{Vendor}{Module}BlockCatalogProductView{Class}" name="{your.block.name}" template="{Vendor}_{Module}::{template_name}.phtml">
                <!-- If you have any arguments to pass -->
                <arguments>
                <argument translate="true" name="{argument_name}" xsi:type="string">{Your argument}</argument>
                </arguments>
                </block>
                </referenceBlock>
                </body>
                </page>





                share|improve this answer












                Try this:



                <?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">
                <block class="{Vendor}{Module}BlockCatalogProductView{Class}" name="{your.block.name}" template="{Vendor}_{Module}::{template_name}.phtml">
                <!-- If you have any arguments to pass -->
                <arguments>
                <argument translate="true" name="{argument_name}" xsi:type="string">{Your argument}</argument>
                </arguments>
                </block>
                </referenceBlock>
                </body>
                </page>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 26 '18 at 6:22









                hungersoft

                2213




                2213












                • For class="{Vendor}{Module}BlockCatalogProductView{Class}" should I create a block class, please post a example for block class.
                  – Vinaya Maheshwari
                  Mar 26 '18 at 6:26










                • You should be able to use this block: MagentoFrameworkViewElementTemplate
                  – hungersoft
                  Mar 26 '18 at 6:27










                • How can I get $product in custom template file?
                  – Vinaya Maheshwari
                  Mar 26 '18 at 9:28










                • Follow this tutorial: blog.chapagain.com.np/…
                  – hungersoft
                  Mar 26 '18 at 9:30


















                • For class="{Vendor}{Module}BlockCatalogProductView{Class}" should I create a block class, please post a example for block class.
                  – Vinaya Maheshwari
                  Mar 26 '18 at 6:26










                • You should be able to use this block: MagentoFrameworkViewElementTemplate
                  – hungersoft
                  Mar 26 '18 at 6:27










                • How can I get $product in custom template file?
                  – Vinaya Maheshwari
                  Mar 26 '18 at 9:28










                • Follow this tutorial: blog.chapagain.com.np/…
                  – hungersoft
                  Mar 26 '18 at 9:30
















                For class="{Vendor}{Module}BlockCatalogProductView{Class}" should I create a block class, please post a example for block class.
                – Vinaya Maheshwari
                Mar 26 '18 at 6:26




                For class="{Vendor}{Module}BlockCatalogProductView{Class}" should I create a block class, please post a example for block class.
                – Vinaya Maheshwari
                Mar 26 '18 at 6:26












                You should be able to use this block: MagentoFrameworkViewElementTemplate
                – hungersoft
                Mar 26 '18 at 6:27




                You should be able to use this block: MagentoFrameworkViewElementTemplate
                – hungersoft
                Mar 26 '18 at 6:27












                How can I get $product in custom template file?
                – Vinaya Maheshwari
                Mar 26 '18 at 9:28




                How can I get $product in custom template file?
                – Vinaya Maheshwari
                Mar 26 '18 at 9:28












                Follow this tutorial: blog.chapagain.com.np/…
                – hungersoft
                Mar 26 '18 at 9:30




                Follow this tutorial: blog.chapagain.com.np/…
                – hungersoft
                Mar 26 '18 at 9:30













                1














                If you want to add this custom block via Custom module,
                Then create catalog_product_view.xml at app/code/{Vendorname}/{moduleName}/view/frontend/layout,.



                <?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">
                <block class="{Vendor}{Module}Block{BLOCK_CLASSS}" name="{block.name.inlayout}" as="{name_aliase_path}"
                template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                </block>
                </referenceBlock>
                </body>
                </page>


                Then you can call your block via at code at form.phtml at



                <?= $block->getChildHtml({name_aliase_path}) ?>





                share|improve this answer























                • Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
                  – inrsaurabh
                  Mar 26 '18 at 6:59
















                1














                If you want to add this custom block via Custom module,
                Then create catalog_product_view.xml at app/code/{Vendorname}/{moduleName}/view/frontend/layout,.



                <?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">
                <block class="{Vendor}{Module}Block{BLOCK_CLASSS}" name="{block.name.inlayout}" as="{name_aliase_path}"
                template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                </block>
                </referenceBlock>
                </body>
                </page>


                Then you can call your block via at code at form.phtml at



                <?= $block->getChildHtml({name_aliase_path}) ?>





                share|improve this answer























                • Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
                  – inrsaurabh
                  Mar 26 '18 at 6:59














                1












                1








                1






                If you want to add this custom block via Custom module,
                Then create catalog_product_view.xml at app/code/{Vendorname}/{moduleName}/view/frontend/layout,.



                <?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">
                <block class="{Vendor}{Module}Block{BLOCK_CLASSS}" name="{block.name.inlayout}" as="{name_aliase_path}"
                template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                </block>
                </referenceBlock>
                </body>
                </page>


                Then you can call your block via at code at form.phtml at



                <?= $block->getChildHtml({name_aliase_path}) ?>





                share|improve this answer














                If you want to add this custom block via Custom module,
                Then create catalog_product_view.xml at app/code/{Vendorname}/{moduleName}/view/frontend/layout,.



                <?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">
                <block class="{Vendor}{Module}Block{BLOCK_CLASSS}" name="{block.name.inlayout}" as="{name_aliase_path}"
                template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                </block>
                </referenceBlock>
                </body>
                </page>


                Then you can call your block via at code at form.phtml at



                <?= $block->getChildHtml({name_aliase_path}) ?>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 26 '18 at 6:33

























                answered Mar 26 '18 at 6:29









                Amit Bera

                57.2k1374170




                57.2k1374170












                • Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
                  – inrsaurabh
                  Mar 26 '18 at 6:59


















                • Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
                  – inrsaurabh
                  Mar 26 '18 at 6:59
















                Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
                – inrsaurabh
                Mar 26 '18 at 6:59




                Our custom phtml file will be called as childHtml of any another phtml or we can only call in that particular layout ( catalog_product_view.xml ) only which we created our custom phtml file
                – inrsaurabh
                Mar 26 '18 at 6:59











                1














                Use this



                <?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">
                <block class="MagentoFrameworkViewElementTemplate" name="block.name" as="block.alias"
                template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                </block>
                </referenceBlock>
                </body>
                </page>





                share|improve this answer


























                  1














                  Use this



                  <?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">
                  <block class="MagentoFrameworkViewElementTemplate" name="block.name" as="block.alias"
                  template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                  </block>
                  </referenceBlock>
                  </body>
                  </page>





                  share|improve this answer
























                    1












                    1








                    1






                    Use this



                    <?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">
                    <block class="MagentoFrameworkViewElementTemplate" name="block.name" as="block.alias"
                    template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                    </block>
                    </referenceBlock>
                    </body>
                    </page>





                    share|improve this answer












                    Use this



                    <?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">
                    <block class="MagentoFrameworkViewElementTemplate" name="block.name" as="block.alias"
                    template="{Vendor}_{ModuleName}::{YourTemaplte}.phtml">
                    </block>
                    </referenceBlock>
                    </body>
                    </page>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 26 '18 at 6:34









                    Kishan Patadia

                    3,5201923




                    3,5201923























                        0














                         <referenceContainer name="product.info.main">
                        <block class="MagentoFrameworkViewElementTemplate" name="{argument_name}" template="template_name.phtml"/>
                        </referenceContainer>





                        share|improve this answer


























                          0














                           <referenceContainer name="product.info.main">
                          <block class="MagentoFrameworkViewElementTemplate" name="{argument_name}" template="template_name.phtml"/>
                          </referenceContainer>





                          share|improve this answer
























                            0












                            0








                            0






                             <referenceContainer name="product.info.main">
                            <block class="MagentoFrameworkViewElementTemplate" name="{argument_name}" template="template_name.phtml"/>
                            </referenceContainer>





                            share|improve this answer












                             <referenceContainer name="product.info.main">
                            <block class="MagentoFrameworkViewElementTemplate" name="{argument_name}" template="template_name.phtml"/>
                            </referenceContainer>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 26 '18 at 7:19









                            hweb87

                            5381723




                            5381723























                                0














                                <referenceContainer name="content">

                                <block class="XcommerceCategoryBlockOverview" name="overview"
                                template="X2commerce_Category::custom.phtml">
                                </block>
                                </referenceContainer>





                                share|improve this answer


























                                  0














                                  <referenceContainer name="content">

                                  <block class="XcommerceCategoryBlockOverview" name="overview"
                                  template="X2commerce_Category::custom.phtml">
                                  </block>
                                  </referenceContainer>





                                  share|improve this answer
























                                    0












                                    0








                                    0






                                    <referenceContainer name="content">

                                    <block class="XcommerceCategoryBlockOverview" name="overview"
                                    template="X2commerce_Category::custom.phtml">
                                    </block>
                                    </referenceContainer>





                                    share|improve this answer












                                    <referenceContainer name="content">

                                    <block class="XcommerceCategoryBlockOverview" name="overview"
                                    template="X2commerce_Category::custom.phtml">
                                    </block>
                                    </referenceContainer>






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered yesterday









                                    hitesh balpande

                                    3089




                                    3089






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Magento Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f219813%2fmagento2-how-to-add-custom-block-on-product-page%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        An IMO inspired problem

                                        Management

                                        Has there ever been an instance of an active nuclear power plant within or near a war zone?