0

How to override vendor/magento/module-catalog/Block/Product/View/Options/Type/Select.php file

I have followed below steps, but not working Step1: Created di.xml

 <preference for="Magento\Catalog\Block\Product\View\Options\Type\Select" type="Vendor\Catalog\Block\Product\View\Options\Type\Select" />

Step2: created

app/code/Vendor/Catalog/Block/Product/View/Options/Type/Select.php

<?php

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

class Select extends \Magento\Catalog\Block\Product\View\Options\Type\Select
{


    public function getValuesHtml()
    {  


    }
}
0

You need to do following things.

  1. Create di.xml file at following place.

    app\code\Vendor\Extension\etc\di.xml

    with code

"

<preference for="Magento\Catalog\Block\Product\View\Options\Type\Select"
type="Vendor\Extension\Block\Product\View\Options\Type\Select"/>

"

  1. Create Block file at following location.

    app\code\Vendor\Extension\Block\Product\View\Options\Type\Select.php

    <?php
    namespace Vendor\Extension\Block\Product\View\Options\Type;
    
    use Magento\Catalog\Block\Product\View\Options\Type\Select as TypeSelect;
    
    class Select extends TypeSelect
    {
       public function getValuesHtml()
        {
        // Do your custom code here
        }
    }
    
  • I have followed above steps but still not working – Sudheer Singamsetti Sep 1 '17 at 10:20
  • the above steps is not working. – gajjala sandeep Sep 1 '17 at 10:21
  • I already do this things in my one extension. Please verify path specify in di.xml. In your code you forgot to mension Extension name. – Dhiren Vasoya Sep 1 '17 at 10:22
  • Hi Dhiren , thanks for quick reply and have u overrided same file ? – gajjala sandeep Sep 1 '17 at 10:25
  • yes, I overide same file in my custom extension. So I posted answer here. – Dhiren Vasoya Sep 2 '17 at 10:52
0

After a long struggle found the answer.
This is what I did to override block \Magento\Catalog\Block\Product\View\Options\Type\Select.php

File - [Vendor][Module]\etc\di.xml

<type name="Magento\Catalog\Block\Product\View\Options\Type\Select">
    <plugin name="[Vendor]_[Module]_select" sortOrder="10"
            type="[Vendor]\[Module]\Block\Product\View\Options\Type\Select"/>
</type>

File - [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\AbstractOptions
{

 public function afterGetValuesHtml(\Magento\Catalog\Block\Product\View\Options\Type\Select $subject, $result)
 {
    $_option = $subject->getOption();
    $configValue = $subject->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
    $store = $subject->getProduct()->getStore();

    $subject->setSkipJsReloadPrice(1);
    // Remove inline prototype onclick and onchange events

    if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DROP_DOWN ||
        $_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_MULTIPLE
    ) {
        $require = $_option->getIsRequire() ? ' required' : '';

        $extraParams = '';
        $select = $subject->getLayout()->createBlock(
            'Magento\Framework\View\Element\Html\Select'
        )->setData(
            [
                'id' => 'select_' . $_option->getId(),
                'class' => $require . ' product-custom-option admin__control-select'
            ]
        );
        if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DROP_DOWN) {
            // Code changes start
            if($_option->getIsSize()){
                $select->setName('options[' . $_option->getid() . ']')->addOption('', __('Select a Size'));
            }
            else if($_option->getIsCut()){
                $select->setName('options[' . $_option->getid() . ']')->addOption('', __('Select a Cut'));
            }
            else{
                $select->setName('options[' . $_option->getid() . ']')->addOption('', __('Select an Option'));
            }
            // Code changes end
        } else {
            $select->setName('options[' . $_option->getid() . '][]');
            $select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option');
        }
        foreach ($_option->getValues() as $_value) {
            $priceStr = $subject->_formatPrice(
                [
                    'is_percent' => $_value->getPriceType() == 'percent',
                    'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
                ],
                false
            );
            $select->addOption(
                $_value->getOptionTypeId(),
                $_value->getTitle() . ' ' . strip_tags($priceStr) . '',
                ['price' => $subject->pricingHelper->currencyByStore($_value->getPrice(true), $store, false)]
            );
        }
        if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_MULTIPLE) {
            $extraParams = ' multiple="multiple"';
        }
        if (!$subject->getSkipJsReloadPrice()) {
            $extraParams .= ' onchange="opConfig.reloadPrice()"';
        }
        $extraParams .= ' data-selector="' . $select->getName() . '"';
        $select->setExtraParams($extraParams);

        if ($configValue) {
            $select->setValue($configValue);
        }

        return $select->getHtml();
    }

    if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_RADIO ||
        $_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_CHECKBOX
    ) {
        $selectHtml = '<div class="options-list nested" id="options-' . $_option->getId() . '-list">';
        $require = $_option->getIsRequire() ? ' required' : '';
        $arraySign = '';
        switch ($_option->getType()) {
            case \Magento\Catalog\Model\Product\Option::OPTION_TYPE_RADIO:
                $type = 'radio';
                $class = 'radio admin__control-radio';
                if (!$_option->getIsRequire()) {
                    $selectHtml .= '<div class="field choice admin__field admin__field-option">' .
                        '<input type="radio" id="options_' .
                        $_option->getId() .
                        '" class="' .
                        $class .
                        ' product-custom-option" name="options[' .
                        $_option->getId() .
                        ']"' .
                        ' data-selector="options[' . $_option->getId() . ']"' .
                        ($subject->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
                        ' value="" checked="checked" /><label class="label admin__field-label" for="options_' .
                        $_option->getId() .
                        '"><span>' .
                        __('None') . '</span></label></div>';
                }
                break;
            case \Magento\Catalog\Model\Product\Option::OPTION_TYPE_CHECKBOX:
                $type = 'checkbox';
                $class = 'checkbox admin__control-checkbox';
                $arraySign = '[]';
                break;
        }
        $count = 1;
        foreach ($_option->getValues() as $_value) {
            $count++;

            $priceStr = $subject->_formatPrice(
                [
                    'is_percent' => $_value->getPriceType() == 'percent',
                    'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
                ]
            );

            $htmlValue = $_value->getOptionTypeId();
            if ($arraySign) {
                $checked = is_array($configValue) && in_array($htmlValue, $configValue) ? 'checked' : '';
            } else {
                $checked = $configValue == $htmlValue ? 'checked' : '';
            }

            $dataSelector = 'options[' . $_option->getId() . ']';
            if ($arraySign) {
                $dataSelector .= '[' . $htmlValue . ']';
            }

            $selectHtml .= '<div class="field choice admin__field admin__field-option' .
                $require .
                '">' .
                '<input type="' .
                $type .
                '" class="' .
                $class .
                ' ' .
                $require .
                ' product-custom-option"' .
                ($subject->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
                ' name="options[' .
                $_option->getId() .
                ']' .
                $arraySign .
                '" id="options_' .
                $_option->getId() .
                '_' .
                $count .
                '" value="' .
                $htmlValue .
                '" ' .
                $checked .
                ' data-selector="' . $dataSelector . '"' .
                ' price="' .
                $subject->pricingHelper->currencyByStore($_value->getPrice(true), $store, false) .
                '" />' .
                '<label class="label admin__field-label" for="options_' .
                $_option->getId() .
                '_' .
                $count .
                '"><span>' .
                $_value->getTitle() .
                '</span> ' .
                $priceStr .
                '</label>';
            $selectHtml .= '</div>';
        }
        $selectHtml .= '</div>';

        return $selectHtml;
    }
 }
}

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.

  1. Change the namespace to [Vendor]\[Module]\Block\Product\View\Options\Type;
  2. Replace public function getValuesHtml() to public function afterGetValuesHtml(\Magento\Catalog\Block\Product\View\Options\Type\Select $subject, $result)
  3. Replace $this to $subject

Hope this helps.

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.