0

I tried to change the template of the text custom option in frontend.

I copied the original

/vendor/magento/module-catalog/view/frontend/templates/product/view/options/type/text.phtml

to

Vendor/Module/view/frontend/templates/catalog/product/view/options/type/text.phtml.

and add to

Vendor/Module/view/frontend/layout/catalog_product_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <referenceBlock name="product.info.options">
                <action method="setTemplate">
                    <argument name="text" xsi:type="string">Vendor_Module::catalog/product/view/options/type/text.phtml</argument>
                </action>
            </referenceBlock>
        </referenceContainer>
    </body>
</page>

my custom.phtml file is well called, but craches at the first lines :

<?php
    $_option = $block->getOption();
    $class = ($_option->getIsRequire()) ? ' required' : '';
?>

$_option being null after $_option = $block->getOption();

Thank you for your help,

EDIT 1:

I tried for catalog_product_view.xml

 <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.options">
                <referenceBlock name="text">
                    <action method="setTemplate">
                        <argument name="template" xsi:type="string">Vendor_Module::catalog/product/view/options/type/text.phtml</argument>
                    </action>
                </referenceBlock>
    </referenceBlock>
        </body>
    </page>

but it is not working...

2
  • why if $_option is null means that it crash? Commented Jul 21, 2017 at 7:03
  • Is get_class($block) the same with your template and default template? Commented Jul 21, 2017 at 7:04

4 Answers 4

7

After hours searching on internet, it is apparently not possible to change Custom Options templates using the standard XML way...

Therefore, here is a clean solution using Plugin :

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Catalog\Block\Product\View\Options\Type\Text">
        <plugin name="my_module_change_cotexttemplate" type="My\Module\Plugin\Catalog\Product\View\Options\TextCustOptionPlugin" sortOrder="1"/>
    </type>
</config>

My\Module\Plugin\Catalog\Product\View\Options\TextCustOptionPlugin.php

<?php

namespace My\Module\Plugin\Catalog\Product\View\Options;


class TextCustOptionPlugin
{
    public function beforeSetTemplate(
        \Magento\Catalog\Block\Product\View\Options\Type\Text $subject,
        $template
    ) 
    {
         return ['My_Module::catalog/product/view/options/type/text.phtml'];
    }
}

if it can help !

2
  • its working for me !!! Thanks +1 up Commented Feb 14, 2019 at 11:09
  • But this is not working in Enterprise edition Magento2.2.0. Commented May 1, 2019 at 6:21
0

I'm using this code to override template, it's a bit different from yours

<!-- cambio il template del blocco head -->
<referenceBlock name="html_head">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_Module::html/head.phtml</argument>
    </action>
</referenceBlock>

Without referenceContainer tag and change name="text" to name="template".

6
  • Thank you for your answer but I do not manage to make it works. Please see my update Commented Jul 21, 2017 at 10:10
  • why do you use <referenceBlock name="text">? Commented Jul 21, 2017 at 14:54
  • If i understand correctly, the <action method="setTemplate">..</action> chnage the template of the block selected by <referenceBlock>. I think the problem is coming from the fact that custom options block do not have a "name" attribute but a "as" attribute... Commented Jul 22, 2017 at 5:09
  • i don't know what are you doing, my code works correctly and override magento template with custom template. "as" property is for magento1 i never see it in magento2. Commented Jul 23, 2017 at 20:23
  • Yes, your code work perfectly except for custom options templates... "as" is used in magento2 (cf vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml line70) Commented Jul 25, 2017 at 6:57
0

Write below at catalog_product_view.xml :

<page layout="1column" 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.options">

            <block class="Emipro\Customoption\Block\Product\View\Options\Type\Text" as="text" template="Emipro_Customoption::product/view/options/type/text.phtml"/>
        </referenceBlock>
    </body>
</page>
0

I got some custom module to develop new feature onto. And it was an override block's template by plugin. It's no a good way. Plugin works on class instance. It works on each block that extended \Magento\Catalog\Block\Product\View\Options\Type\Text

The best way is override it by layout xml file. All suggestion above were wrong because try to use alias. Block and referenceBlock require name.

Open the xml when Text option field starts on Magento. It's vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml

<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>

Ok! Let's do required override in custom module app/code/VendorName/ModuleName/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page layout="1column" 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.options">
            <referenceBlock name="product.info.options.text">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">VendorName_ModuleName::catalog/product/view/options/type/text.phtml</argument>
                </action>
            </referenceBlock>
        </referenceBlock>
    </body>
</page>

It works for me. And you can override it again if it needs. Plugin example above doesn't allow you for it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.