1

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"/>
</block>

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

I tried the following:

MyCompany/MyModule/view/frontend/layout/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>
        <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"/>
            </referenceBlock>
         </referenceContainer>
    </body>
</page>

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
0

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.

Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);

Vendor/Module/etc/module.xml

<?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">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Vendor/Module/etc/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">
    <preference for="Magento\Catalog\Block\Product\View\Options\Type\Select" type="Vendor\Module\Block\Product\View\Options\Type\Select" />
</config>

Vendor/Module/Block/Product/View/Options/Type/Select.php

<?php

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';
}

Vendor/Module/view/frontend/templates/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.