Here is the way to the solution :
1. Create the module
2. Declare your new product option : app/code/Vendor/Module/etc/product_options.xml
3. Add the new custom Option in Admin/Product :
- Vendor\Module\Block\Adminhtml\Catalog\Product\Edit\Tab\Options\Type\Newtype.php
- Vendor\Module\Block\Adminhtml\Catalog\Product\Edit\Tab\Options\Option.php
to define the template
- create the associated .phtml files
Vendor/Module/view/adminhtml/templates/catalog/product/edit/options/type/newtype.phtml
and
Vendor/Module/view/adminhtml/templates/catalog/product/edit/options/option.phtml
4. Add the new custom Option in Frontend Catalog :
- Vendor\Module\Block\Catalog\Product\View\Options\Type\Newtype.php;
- The associated template : Vendor/Module/view/frontend/templates/catalog/product/view/options/type/newtype.phtml
- The layout definition : Vendor\Module\view\frontend\layout\catalog_product_view.xml
5. the Models for all that
- Vendor\Module\Model\Catalog\Product\Option\Type\Newtype.php
- Vendor\Module\Model\Catalog\Product\Option.php
- Vendor\Module\Model\Catalog\ResourceModel\Product\Option.php
6. Extend Ui :
- Vendor\Module\Ui\DataProvider\Catalog\Product\Form\Modifier\CustomOptions.php
Hope this help !
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.
Best Answer
Create a plugin for that. So you need to create new module Or add following code into your existing module.
SR/StackExchange/etc/adminhtml/di.xml
SR/StackExchange/Plugin/Catalog/Model/Config/Source/Product/Options/Price.php
Clear magento cache.