As of now community edition only have the option for creating product attribute from admin side. But for creating custom magento2 catalog , customer attribute is not available in admin side . So either we will use custom script or create a new module for creating custom magento2 attribute. Also I have given a link of sample module through which you can get custom module to create custom magento2 attribute for category, customer.
I have given three example for creating product, catalog and customer attribute . Then you can use it either by custom script or through your custom module to execute the query to create custom magento2 attribute. I have also provided another article that is in magento1 for creating different attribute( How to create attribute by custom script mageneto1 ) .For more details you can refer to magento1 article, but here I am giving another article for magento2 as magento2 structure has been changed .
Product Attribute:
Use entity: \Magento\Catalog\Model\Product::ENTITY
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'custom_product_attribute', [ 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'Custom Prodct Atrribute', 'input' => '', 'class' => '', 'source' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => true, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => '' ] ); |
Category Attribute:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$eavSetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'custom_category_attribute', [ 'type' => 'textarea', 'label' => 'Custom category attribute', 'input' => 'text', 'required' => false, 'sort_order' => 4, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'wysiwyg_enabled' => true, 'is_html_allowed_on_front' => true, 'group' => 'General Information', ] ); |
Customer Attribute:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'custom_customer_attribute', [ 'type' => 'int', 'label' => 'Custom Customer Attribute', 'input' => 'select', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'required' => true, 'default' => '0', 'sort_order' => 100, 'system' => false, 'position' => 100 ] ); } |
for getting full files with custom module, you can download from here