Skip to content

Custom field for product custom options – Magento 2.4

We used the following method, to create a custom field for the product custom option section: https://devhooks.in/blog/magento2-how-to-add-custom-field-in-custom-option

This works fine, but we can not define the value for a store view specific.

How can we modify the code, to save different values for store views?

See our; CustomOptionsPluginCatalogUiDataProviderProductFormModifier

<?php 
namespace ModuleCustomOptionsPluginCatalogUiDataProviderProductFormModifier;

class CustomOptions
{

    public function afterModifyMeta(
        MagentoCatalogUiDataProviderProductFormModifierCustomOptions $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' => __('Description'),
                            'componentType' => MagentoUiComponentFormField::NAME,
                            'formElement' => MagentoUiComponentFormElementTextarea::NAME,
                            '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' => __('Description'),
                            'componentType' => MagentoUiComponentFormField::NAME,
                            'formElement' => MagentoUiComponentFormElementTextarea::NAME,
                            'dataScope' => 'description',
                            'dataType' => MagentoUiComponentFormElementDataTypeText::NAME,
                            'sortOrder' => $sortOrder,
                            'validation' => [
                                'required-entry' => false
                            ],
                        ],
                    ],
                ],
            ],
            $options
        );
    }
}