2

I have created a custom category attribute using the custom code from here, attribute created successfully and able to save the value.

But I want to save store specific value in this custom attribute, So can save different value for English store view and Arabic store view.

I also try another way to create the custom category attribute, but no one saves store specific value.

I also found this issue on GitHub and try the given solution but not one is work for me.

5

4 Answers 4

2

Add below code in category_form.xml

<fieldset name="general">
    <container>
        <field name="my_cat_desc" sortOrder="130" formElement="input">
            <settings>
                <dataType>varchar</dataType>
                <label translate="true">My Cat Description</label>
                <scopeLabel>[STORE VIEW]</scopeLabel>
            </settings>
        </field>
    </container>
</fieldset>

Mynamespace\Mymodule\etc\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\Model\Category\DataProvider">
        <plugin name="category_color_data_mapping" type="Mynamespace\Mymodule\Model\Category\DataProvider" sortOrder="1" disabled="false"/>
    </type>
</config>

Mynamespace\Mymodule\Plugin\Category\DataProvider.php

<?php
namespace Mynamespace\Mymodule\Plugin\Category;

class DataProvider extends \Magento\Catalog\Model\Category\DataProvider
{
    public function __construct(
        Config $eavConfig
    ) {
        $this->eavConfig = $eavConfig;
    }
    
    public function afterPrepareMeta(\Magento\Catalog\Model\Category\DataProvider $subject, $result)
    {
        $meta = $result;
        $meta = array_replace_recursive($meta, $this->prepareFieldsMeta(
            $this->getFieldsMap(),
            $subject->getAttributesMeta($this->eavConfig->getEntityType('catalog_category'))
        ));
        return $meta;

    }
    
    private 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;
    }
    protected function getFieldsMap()
    {
        $fields = '';
        $fields['general'][] = 'my_cat_desc';

        return $fields;
    }
}
1

This almost worked. A few changes in the given code made it work. First change the di.xml line :

Mynamespace\Mymodule\Model\Category\DataProvider

into

Mynamespace\Mymodule\Plugin\Category\DataProvider

Next include a

use Magento\Eav\Model\Config;

for the plugin class Mynamespace\Mymodule\Plugin\Category\DataProvider at the start of the class, add protected $eavConfig; or $this->eavConfig = $eavConfig won't work, and also change

$fields = '';

into

$fields = [];

With these changes I had it fixed. Thanks Nalin Savaliya

0

Please check "is_global" property in the database table catalog_eav_attribute for your custom attribute and make sure it is having value as 0. Then after your custom attribute one checkbox will appear "Use Default Value" in the admin. Unchecking that checkbox will help you to save attribute value store view level.

1
  • That is already set to 0 in catalog_eav_attribute table for my custom attribute, but still no luck. Jun 17, 2019 at 5:15
0

By default magento store the category attributes with the store id. So each category attributes can be save store wise.

In case of custom attributes, may some problem with the creation of attribute. You can go with below article which has a good knowledge base to create the category attribute.

https://www.yereone.com/blog/magento-2-how-to-add-new-category-attribute/

Just make sure you mentioned the below piece of code in proper places:

'global'       => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,

and

<item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>

Also compare your new created attribute with existing one for below tables:

catalog_category_entity_int ( or text or varchar depends on your attribute type)

catalog_eav_attribute

eav_attribute
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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