0

I'm trying to add a custom template before the date element. Here is the code in catalog_product_view.xml on my custom theme.

    <referenceBlock name="product.info.options">
        <block class="Magento\Catalog\Block\Product\View"
  name="custom.text" as="customtext" template="Vendor_Catalog::customtext.phtml" before="product.info.options.date"/>
    </referenceBlock>

and these codes are from the Magento catalog_product_view.xml.

<block class="Magento\Catalog\Block\Product\View" name="product.info" template="Magento_Catalog::product/view/form.phtml" after="alert.urls">
                    <container name="product.info.form.content" as="product_info_form_content">
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart" as="addtocart" template="Magento_Catalog::product/view/addtocart.phtml"/>
                    </container>
                    <block class="Magento\Framework\View\Element\Template" name="product.info.form.options" as="options_container">
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.options.wrapper" as="product_options_wrapper" template="Magento_Catalog::product/view/options/wrapper.phtml">
                            <block class="Magento\Catalog\Block\Product\View\Options" name="product.info.options" as="product_options" template="Magento_Catalog::product/view/options.phtml">
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\DefaultType" name="product.info.options.default" as="default" template="Magento_Catalog::product/view/options/type/default.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\Text" name="product.info.options.text" as="text" template="Magento_Catalog::product/view/options/type/text.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\File" name="product.info.options.file" as="file" template="Magento_Catalog::product/view/options/type/file.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\Select" name="product.info.options.select" as="select" template="Magento_Catalog::product/view/options/type/select.phtml"/>
                                <block class="Magento\Catalog\Block\Product\View\Options\Type\Date" name="product.info.options.date" as="date" template="Magento_Catalog::product/view/options/type/date.phtml"/>
                            </block>
                            <block class="Magento\Framework\View\Element\Html\Calendar" name="html_calendar" as="html_calendar" template="Magento_Theme::js/calendar.phtml"/>
                        </block>
                        <block class="Magento\Catalog\Block\Product\View" name="product.info.options.wrapper.bottom" as="product_options_wrapper_bottom" template="Magento_Catalog::product/view/options/wrapper/bottom.phtml">
                            <block class="Magento\Catalog\Block\Product\View" name="product.info.addtocart.additional" as="product.info.addtocart" template="Magento_Catalog::product/view/addtocart.phtml"/>
                        </block>
                    </block>
                </block>

custom text and the template not showing.

what's wrong the in code?

2 Answers 2

2

your first mistake is you have used before="product.info.options.date" as you should use it's alias when it's exist as="date" instead of name="".

your second mistake is your block is not appear seems may be product.info.options.date block is not exist there.

your third mistake is you should used to define custom block in any page as like below.

    <referenceContainer name="alert.urls">
                <block class="Magento\Catalog\Block\Product\View"
      name="custom.text" as="customtext" template="Vendor_Frontendsample::frontendsample_index_index.phtml" before="addtocart"/>
   </referenceContainer>

try like above code, display your block just above actual block display there.

let me know if you still have any query. you can take reference from here

2
  • I want to add it before the date element. how will i do that?
    – Tora
    Oct 1, 2018 at 5:48
  • try this <referenceContainer name="alert.urls"> <block class="Magento\Catalog\Block\Product\View" name="custom.text" as="customtext" template="Vendor_Catalog::customtext.phtml" before="date"/> </referenceContainer> make sure your custom block is working proper
    – Hitesh
    Oct 1, 2018 at 5:52
1

Here are some corrections you should pay attention to:

<referenceContainer name="product.second.container">
            <block class="Magento\Catalog\Block\Product\View" name="custom.text" as="customtext"
                   template="product/view/options/type/frontendsample_index_index.phtml" before="date" after="select"/>
</referenceContainer>

The frontendsample_index_index.phtml file should be located at app/design/frontend/<Vendor_Theme>/<Theme_Name>/Magento_Catalog/templates/product/view/options/type/frontendsample_index_index.phtml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.