Skip to content

New custom field I added to plugin is not writing to the db

I have a Magento 2.4.6 store. We are using the Mageworks Options Extended plugin. I need to add a field. They have a nice blog about how to do so but it seems from a few years ago: https://www.mageworx.com/blog/how-to-add-custom-field-to-advanced-product-options-extension My code has created the new field in the table on the db, and it has created the input for the field on the admin panel. When I enter a value in the input on the plugin’s admin section and hit save the value is not saved to the db or is it visible in the input after a refresh of the page. Here is the code I used
Any help would be greatly appreciated

#composer.json
{
    "name": "mageworx/module-optiongtin",
    "description": "N/A",
    "require": {
        "magento/framework"     :     ">=100.1.0 <101",
        "magento/module-catalog":     ">=101.0.0 <104"
    },
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "AbbaZabba\OptionGtin\": ""
        }
    }
}

#etc/module.xml
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="AbbaZabba_OptionGtin" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
            <module name="MageWorx_OptionBase"/>
        </sequence>
    </module>
</config>

#registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'AbbaZabba_OptionGtin',
    __DIR__
)
;
Step #2. Add Our New Field to Database

#app/code/AbbaZabba/OptionGtin/Setup/InstallSchema.php

<?php
namespace AbbaZabbaOptionGtinSetup;

use MagentoFrameworkSetupInstallSchemaInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupSchemaSetupInterface;
use MagentoFrameworkDBDdlTable;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $setup->getConnection()->addColumn(
            $setup->getTable('catalog_product_option'),
            'gtin',
            [
                'type'     => Table::TYPE_TEXT,
                'nullable' => true,
                'default'  => null,
                'comment'  => 'Gtin (added by AbbaZabba Option Gtin)',
            ]
        );

        $setup->endSetup();

    }
}

Step #3. Add Logic to Work with Backend
I used the pool-modifier mechanism to add the new field.

app/code/AbbaZabba/OptionGtin/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">
    <virtualType name="MageWorxOptionBaseUiDataProviderProductFormModifierPool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="mageworx-option-gtin" xsi:type="array">
                    <item name="class" xsi:type="string">AbbaZabbaOptionGtinUiDataProviderProductFormModifierOptionGtin</item>
                    <item name="sortOrder" xsi:type="number">72</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

The code that allows adding  field to the app/code/AbbaZabba/OptionGtin/Ui/DataProvider/Product/Form/Modifier/OptionGtin.php form:

<?php
namespace AbbaZabbaOptionGtinUiDataProviderProductFormModifier;

use MagentoCatalogUiDataProviderProductFormModifierAbstractModifier;
use MagentoCatalogUiDataProviderProductFormModifierCustomOptions;
use MagentoUiComponentFormElementInput;
use MagentoUiComponentFormElementDataTypeNumber;
use MagentoUiComponentFormField;
use MageWorxOptionBaseUiDataProviderProductFormModifierModifierInterface;

class OptionGtin extends AbstractModifier implements ModifierInterface
{
    /**
     * @var array
     */
    protected $meta = [];

    /**
     * {@inheritdoc}
     */
    public function modifyData(array $data)
    {
        return $data;
    }

    /**
     * {@inheritdoc}
     */
    public function modifyMeta(array $meta)
    {
        $this->meta = $meta;

        $this->addFields();

        return $this->meta;
    }

    /**
     * Adds fields to the meta-data
     */
    protected function addFields()
    {

        $groupCustomOptionsName    = CustomOptions::GROUP_CUSTOM_OPTIONS_NAME;
        $optionContainerName       = CustomOptions::CONTAINER_OPTION;
        $commonOptionContainerName = CustomOptions::CONTAINER_COMMON_NAME;

        // Add fields to the option
        $optionFeaturesFields  = $this->getOptionGtinFieldsConfig();

        $this->meta[$groupCustomOptionsName]['children']['options']['children']['record']['children']
        [$optionContainerName]['children'][$commonOptionContainerName]['children'] = array_replace_recursive(
            $this->meta[$groupCustomOptionsName]['children']['options']['children']['record']['children']
            [$optionContainerName]['children'][$commonOptionContainerName]['children'],
            $optionFeaturesFields
        );

    }

    /**
     * The custom option fields config
     *
     * @return array
     */
    protected function getOptionGtinFieldsConfig()
    {
        $fields['gtin'] = $this->getGtinFieldConfig();

        return $fields;
    }

    /**
     * Get gtin field config
     *
     * @return array
     */
    protected function getGtinFieldConfig()
    {
        return [
            'arguments' => [
                'data' => [
                    'config' => [
                        'label'         => __('GTIN'),
                        'componentType' => Field::NAME,
                        'formElement'   => Input::NAME,
                        'dataType'      => Number::NAME,
                        'dataScope'     => 'gtin',
                        'sortOrder'     => 65
                    ],
                ],
            ],
        ];
    }

    /**
     * Check is current modifier for the product only
     *
     * @return bool
     */
    public function isProductScopeOnly()
    {
        return false;
    }

    /**
     * Get sort order of modifier to load modifiers in the right order
     *
     * @return int
     */
    public function getSortOrder()
    {
        return 32;
    }
}


php bin/magento module:enable AbbaZabba_OptionGtin
php bin/magento setup:upgrade
php bin/magento cache:flush
Our new field has been added successfully: