Magento 2 Custom Options – How to Add New Text Field in Custom Options Section

custom-optionsmagento2product

How can I add a new text field in the custom options section in Magento 2 as shown in the screenshot below?

screenshot

Best Answer

Answer: I got the answer for the above question to add the text field by simply editing the customOptions.php file

Location:vendor/magento/module- catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

step1: Add your text field at line number:68

const FIELD_NEW_TEXT_NAME = 'new_text_field';

step2:Add the Children at line number:584,634

static::FIELD_NEW_TEXT_NAME => $this->getNewTextFieldConfig(41),

step3:Copy the Total function code of getMaxCharactersFieldConfig and Create the function at line number:936

by following code:

protected function getNewTextFieldConfig($sortOrder)
 {
    return [
        'arguments' => [
            'data' => [
                'config' => [
                    'label' => __('New Text field'),
                    'componentType' => Field::NAME,
                    'formElement' => Input::NAME,
                    'dataScope' => static::FIELD_NEW_TEXT_NAME,
                    'dataType' => Number::NAME,
                    'sortOrder' => $sortOrder,
                    'validation' => [
                        'validate-zero-or-greater' => true
                    ],
                ],
            ],
        ],
    ];
  }

step 4:Created the respected columns for fields in database find the table name below:

catalog_product_option catalog_product_option_type_value

Step 5:clear the cache and check the out put.

Related Topic