0

I'am following this tutorial https://www.mageplaza.com/devdocs/magento-2-category-attributes-programmatically/ added the field to save new attribute but not working, write the information but to save in button top, refresh the page and not save the information. I'm using magento 2.4

new field

1
  • You are using Magento 2.4 don't use the old approach to create product attributes. Use the new way to create an attribute rohanhapani.com/…
    – Avesh Naik
    Aug 28, 2021 at 7:59

1 Answer 1

0

First of all check if your data is saved in the database, if that's so it's just a dataProvider issue and you can solve it with a plugin

Declare your Plugin

<type name="Magento\Catalog\Model\Category\DataProvider">
            <plugin name="categoryCustomAttributes" type="Vendor\Module\Plugin\Model\Category\DataProvider" sortOrder="1" disabled="false"/>
        </type>

Then create the Plugin:

namespace Vendor\Module\Plugin\Model\Category;


class DataProvider extends \Magento\Catalog\Model\Category\DataProvider {

public function __construct(\Magento\Eav\Model\Config $eavConfig) {
    $this->eavConfig = $eavConfig;
}

public function afterPrepareMeta(\Magento\Catalog\Model\Category\DataProvider $subject, $result) {
    $meta = array_replace_recursive($result, $this->_prepareFieldsMeta(
        $this->_getFieldsMap(),
        $subject->getAttributesMeta($this->eavConfig->getEntityType('catalog_category'))
    ));
    return $meta;
}

public function _prepareFieldsMeta($fieldsMap, $fieldsMeta) {
    $result = [];
    foreach ($fieldsMap as $fieldSet => $fields) {
        foreach ($fields as $field) {
            if (isset($fieldsMeta[$field])) {
                $result[$fieldSet]['children'][$field]['arguments']['data']['config'] = $fieldsMeta[$field];
            }
          }
        }
        return $result;
    }

    public function _getFieldsMap() {
       $fields = parent::getFieldsMap();
       $fields['general'][] = 'custom_attribute';
       return $fields;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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