7

I would like to add new custom option text field like 'New Text Field Title' in the magento 2.x 'Customizable Options' section could you please suggest me and guide me how to add please refer my screenshot. enter image description here

  • I got this plugin working properly and saving, etc. I have also created a plugin which allows me to override Select.php. That said, Can someone tell me how to display the new custom options field in Select.php? I need my custom option titles/labels to look like this: <label class="label" for="select_4567" class="my_custom_options_text"> <span>My Custom Options Title</span> </label> – user3254623 Apr 30 '19 at 22:25
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – Jai May 1 '19 at 1:23
13

Create a plugin for that. Try following way:

SR/StackExchange/etc/adminhtml/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">
    <type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions">
        <plugin name="sr_stackexchange_custom_option" type="SR\StackExchange\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions" sortOrder="1"/>
    </type>
</config>

SR/StackExchange/Plugin/Catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

namespace SR\StackExchange\Plugin\Catalog\Ui\DataProvider\Product\Form\Modifier;


class CustomOptions
{

    public function afterModifyMeta(
        \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\CustomOptions $subject,
        $meta
    ) {
        $meta['custom_options']['children']['options']['children']['record']['children']['container_option']['children']['container_common']['children']['custom_text'] =
        $this->getTitleFieldConfig(
            200,
            [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'label' => __('Custom Text'),
                            'component' => 'Magento_Catalog/component/static-type-input',
                            'valueUpdate' => 'input',
                            'imports' => [
                                'optionId' => '${ $.provider }:${ $.parentScope }.option_id'
                            ]
                        ],
                    ],
                ],
            ]
        );
        return $meta;
    }

    /**
     * Get config for "Title" fields
     *
     * @param int $sortOrder
     * @param array $options
     * @return array
     */
    protected function getTitleFieldConfig($sortOrder, array $options = [])
    {
        return array_replace_recursive(
            [
                'arguments' => [
                    'data' => [
                        'config' => [
                            'label' => __('Custom Text'),
                            'componentType' => \Magento\Ui\Component\Form\Field::NAME,
                            'formElement' => \Magento\Ui\Component\Form\Element\Input::NAME,
                            'dataScope' => 'custom_text',
                            'dataType' => \Magento\Ui\Component\Form\Element\DataType\Text::NAME,
                            'sortOrder' => $sortOrder,
                            'validation' => [
                                'required-entry' => false
                            ],
                        ],
                    ],
                ],
            ],
            $options
        );
    }
}

Clear Magento cache.

NB: This solution only for M2.1.x. and greater.

[Update]

How to save?

-> create a column 'catalog_product_option' table same as field name. e.g: custom_text

-> clear cache.

| improve this answer | |
  • Thanks its showing in the admin side custom options section now but the value unable to save whether we have to do anything ? – Nagaraju K Jan 19 '17 at 6:08
  • we followed the link but we were unable to save the value whether we have to do any database changes? could you plz explain in detail thanks – Nagaraju K Jan 19 '17 at 8:43
  • Check last update code. – Sohel Rana Jan 19 '17 at 9:12
  • 2
    I have create custom field for bundle options, Save successfully into catelog_product_bundle_options table but not loading it after save while edit product. any idea? – Ronak Chauhan Jul 25 '17 at 10:11

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