How can I overwrite the template of something like select.phtml

<block class="Magento\Catalog\Block\Product\View\Options" name="product.info.options" as="product_options" template="product/view/options.phtml">
    <block class="Magento\Catalog\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select.phtml"/>

via my module, even tough it does not have a "name" attribute only a class and "as"?

I tried the following:


<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <referenceBlock name="product.info.options">
                <block class="Magento\Catalog\Block\Product\View\Options\Type\Select" as="select" template="MyCompany_MyModule::product/view/options/type/select.phtml"/>

but that does not work at all.

  • Have you tried targeting it with the alias? The name attribute is not required in Magento 2 so this happens in quite a few places.
    – Ben Crook
    Dec 22 '16 at 13:44
  • How would I do that?
    – TrytoFly
    Dec 22 '16 at 13:51
  • Check the accepted answer here - magento.stackexchange.com/questions/86188/….
    – Ben Crook
    Dec 22 '16 at 14:02
  • In the accepted answer does not work here, because it references the block by the "name" attribute. <referenceBlock name="block_to_change">
    – TrytoFly
    Dec 22 '16 at 15:14
  • Yeah I was 50/50 on whether it would work. If you only want to change the markup you could just overwrite the template, you wouldn't even need to know the name for that - devdocs.magento.com/guides/v2.1/frontend-dev-guide/templates/…
    – Ben Crook
    Dec 22 '16 at 15:16

I finally figured out how to do this so I thought I would share my solution for overriding a template file for a block without a name only alias. I will walk through the entire solution in case anyone else is looking for a similar solution.




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.1">
            <module name="Magento_Catalog"/>


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\View\Options\Type\Select" type="Vendor\Module\Block\Product\View\Options\Type\Select" />



namespace Vendor\Module\Block\Product\View\Options\Type;

class Select extends \Magento\Catalog\Block\Product\View\Options\Type\Select
    protected $_template = 'Vendor_Module::product/view/options/type/select.phtml';


Make the changes you are needing in the phtml file. That should be it!

Enable the module, setup:upgrade, compile, deploy static content, clear cache, etc and you will see the changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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