SOLVED :: 15.9.17 (the following code works on a fresh Magento 2.1.7 installation; tested on Luma theme)
I am struggling with the override of this particular core block
Magento\Catalog\Block\Product\View\Options\Type\Select
Here is my module structure:
├── <VendorName>
| ├── <ModuleName>
| | ├── Block/
| | | ├── Product/
| | | | ├── View/
| | | | | ├── Options/
| | | | | | ├── Type/
| | | | | | | └── Select.php
| | ├── etc/
| | | └── di.xml
| | | └── module.xml
| | ├── view/
| | | ├── frontend/
| | | | ├── templates/
| | | | | ├── product/
| | | | | | ├── view/
| | | | | | | ├── options/
| | | | | | | | ├── type/
| | | | | | | | | └── select.phtml
| | └── registration.php
The registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
The 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="VendorName_ModuleName" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
The 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="VendorName\ModuleName\Block\Product\View\Options\Type\Select" />
</config>
The select.phtml (copied as it is from vendor/magento folder)
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\Options\Type\Select */ ?>
<?php
$_option = $block->getOption();
$class = ($_option->getIsRequire()) ? ' required' : '';
?>
<div class="field<?php /* @escapeNotVerified */ echo $class; ?>">
<label class="label" for="select_<?php /* @escapeNotVerified */ echo $_option->getId() ?>">
<span><?php echo $block->escapeHtml($_option->getTitle()) ?></span>
</label>
<div class="control">
<?php echo $block->getValuesHtml() ?>
<?php if ($_option->getIsRequire()): ?>
<?php if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_RADIO || $_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_CHECKBOX): ?>
<span id="options-<?php /* @escapeNotVerified */ echo $_option->getId() ?>-container"></span>
<?php endif; ?>
<?php endif;?>
</div>
</div>
The Select.php (I removed the custom logic here, as I think not relevant in this context)
<?php
namespace VendorName\ModuleName\Block\Product\View\Options\Type;
class Select extends \Magento\Catalog\Block\Product\View\Options\AbstractOptions
{
public function getValuesHtml()
{
// My Custom Code
}
}
According to https://magento.stackexchange.com/a/112847/51071 I was expecting to succesfully override the block after having activated the module and run setup:di:compile, but nothing happened.
Does anyone has a clue what am I missing here in order to make the module working properly? Any help is much appreciated.
Best Answer
Above answers helped me but it did not work for me as it is.
This is what I did to override block \Magento\Catalog\Block\Product\View\Options\Type\Select.php
File - [Vendor][Module]\etc\di.xml
File - [Vendor][Module]\Block\Product\View\Options\Type\Select.php
Important things to note here -
After copying \Magento\Catalog\Block\Product\View\Options\Type\Select.php to [Vendor][Module]\Block\Product\View\Options\Type\Select.php, you must do following.
[Vendor]\[Module]\Block\Product\View\Options\Type;
public function getValuesHtml()
topublic function afterGetValuesHtml(\Magento\Catalog\Block\Product\View\Options\Type\Select $subject, $result)
$this
to$subject
Hope this helps.