0

I managed to create my custom attribute:

    $installer = $setup;
    $installer->startSetup();
    $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

    $categorySetup->addAttribute(
        Category::ENTITY,
        'content_category_page',
        [
            'type' => 'int',
            'label' => 'Content Page',
            'input' => 'select',
            'sort_order' => 100,
            'source' => 'Magento\Catalog\Model\Category\Attribute\Source\Page',
            'global' => 2,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => null,
            'group' => 'General Information',
            'backend' => ''
        ]
    );
    $installer->endSetup();

I made the file view/adminhtml/ui_component/category_form.xml with this content:

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="content">
        <field name="content_category_page">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Vendor\Module\Model\Config\ContentPages</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Content Page</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="dataScope" xsi:type="string">content_category_page</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

The new attribute appeared in the right place, but when i choose an option and press the save button, the value is not being saved . Would you please advice, why my custom category attribute is not created ? :) Thnx in advance!

0

this is how you can create Custom Attribute, place this code with your changes under Setup/installData.php

namespace AWstreams\Sonic\Setup;
    use Magento\Customer\Setup\CustomerSetupFactory;
    use Magento\Customer\Model\Customer;
    use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
    use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    class InstallData implements InstallDataInterface
        {
        /**
         * @var CustomerSetupFactory
         */
        protected $customerSetupFactory;
        /**
         * @var AttributeSetFactory
         */
        private $attributeSetFactory;
        /**
         * @param CustomerSetupFactory $customerSetupFactory
         * @param AttributeSetFactory $attributeSetFactory
         */
        public function __construct(
            CustomerSetupFactory $customerSetupFactory,
            AttributeSetFactory $attributeSetFactory
        ) {
            $this->customerSetupFactory = $customerSetupFactory;
            $this->attributeSetFactory = $attributeSetFactory;
        }
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
            {
            /*customersetupfactory instead of eavsetupfactory */
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
                $attributeSetId = $customerEntity->getDefaultAttributeSetId();
            /** @var $attributeSet AttributeSet */
                    $attributeSet = $this->attributeSetFactory->create();
                    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
                        /* create customer merchant attribute */
                        $customerSetup->addAttribute(Customer::ENTITY,'customer_code',  [
                        'type'         => 'varchar',
                        'label'        => 'Sonic Customer Code',
                        'input'        => 'text',
                        'source' => '',
                        'required'     => false,
                        'visible'      => false,
                        'default' => '0',
                        'user_defined' => true,
                        'position'     => 332,
                        'sort_order'  => 332,
                        'system'       => 0,
                        'is_used_in_grid' => true,
                        'backend' => '',
                        ]
                        );
                    $sampleAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_code')
                    ->addData(
                    [
                                'attribute_set_id' => $attributeSetId,
                                'attribute_group_id' => $attributeGroupId,
                                'used_in_forms' => ['adminhtml_customer','customer_account_edit','customer_account_create'],
                            ]
                    );
                    $sampleAttribute->save();
                    }
            }

after that, run

setup:upgrade command

| improve this answer | |
  • hmm this looks like to be a customer attribute not a category attribute. I need to use the UpgradeData since i already have a version. – Attila Naghi Jul 1 at 13:36

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.