EAV system in Magento 2 almost similar to Magento 1 but many function and technique is changed , in this tutorials we give you 2 example of how adding custom eav attribute to category or customer .
Download or Explore code example in github
1 – Create custom attribute for category :
A - For Magento 2.1 and higher
Go to your model and create Setup folder , inside it create file InstallData.php and push this full code :
<?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Ibnab\CustomAttribute\Setup; use Magento\Framework\Module\Setup\Migration; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Catalog\Setup\CategorySetupFactory; /** * @codeCoverageIgnore */ class InstallData implements InstallDataInterface { /** * Category setup factory * * @var CategorySetupFactory */ private $categorySetupFactory; /** * Init * * @param CategorySetupFactory $categorySetupFactory */ public function __construct(CategorySetupFactory $categorySetupFactory) { $this->categorySetupFactory = $categorySetupFactory; } /** * {@inheritdoc} * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId); $categorySetup->removeAttribute( \Magento\Catalog\Model\Category::ENTITY, 'my_attribute' ); $categorySetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'my_attribute', [ 'type' => 'int', 'label' => 'My Atrribute ', 'input' => 'select', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'required' => false, 'sort_order' => 100, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'group' => 'General Information', ] ); $installer->endSetup(); } }
After that you need add category_form.xml inside view/adminhtml/ui_component inside your module and push:
B - For Magento 2.07and lower
in this code we injected Magento\Catalog\Setup\CategorySetupFactory and get instance of category setup object with :
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
after that we get the entity type id and atrribute set id :
$entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY); $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
first we remove if exist :
$categorySetup->removeAttribute( \Magento\Catalog\Model\Category::ENTITY, 'my_attribute', );
the \Magento\Catalog\Model\Category::ENTITY is static variable from category model :
const ENTITY = 'catalog_category';
and we add our custom attribute with type field select :
const ENTITY = 'catalog_category';
and we add our custom attribute with type field select :
$categorySetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'my_attribute', [ 'type' => 'int', 'label' => 'My Atrribute ', 'input' => 'select', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'required' => false, 'sort_order' => 100, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'group' => 'General Information', ] );
the source of select box content is 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean' .
You can get id of custom group id for example 'General Information' :
$idg = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'General Information');
and you can change or add the group of this attribute :
$categorySetup->addAttributeToGroup( $entityTypeId, $attributeSetId, $idg, 'my_attribute', 46 );
2 – Create custom attribute for customer :
you can download the module CustomerCustomAtttributePut.zip from github
More explain plz refer to serie Magento 2 Kill EAV For Customers :
http://www.ibnab.com/en/blog/magento-2/magento-2-kill-eav-for-customers-video-courses-part-1
with the same technique got to InstallData.php and push :
<?php namespace Ibnab\CustomerPut\Setup; use Magento\Framework\Module\Setup\Migration; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; class InstallData implements InstallDataInterface { /** * Customer setup factory * * @var \Magento\Customer\Setup\CustomerSetupFactory */ private $customerSetupFactory; /** * Init * * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory */ public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory) { $this->customerSetupFactory = $customerSetupFactory; } /** * Installs DB schema for a module * * @param ModuleDataSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY); $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "my_attribute"); "type" => "varchar", "backend" => "", "label" => "My Attribute", "input" => "text", "source" => "", "visible" => true, "required" => true, "default" => "", "frontend" => "", "unique" => false, "note" => "" )); $my_attribute = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "my_attribute"); $my_attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, ' my_attribute'); $used_in_forms[]="adminhtml_customer"; $used_in_forms[]="checkout_register"; $used_in_forms[]="customer_account_create"; $used_in_forms[]="customer_account_edit"; $used_in_forms[]="adminhtml_checkout"; $my_attribute->setData("used_in_forms", $used_in_forms) ->setData("is_used_for_customer_segment", true) ->setData("is_system", 0) ->setData("is_user_defined", 1) ->setData("is_visible", 1) ->setData("sort_order", 100); $my_attribute->save(); $installer->endSetup(); } }
here we added new code we get instance of attribute :
$my_attribute = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "my_attribute");
and we add to custom form as adminhtml_customer,checkout_register" :
$my_attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, ' my_attribute'); $used_in_forms[]="adminhtml_customer"; $used_in_forms[]="checkout_register"; $used_in_forms[]="customer_account_create"; $used_in_forms[]="customer_account_edit"; $used_in_forms[]="adminhtml_checkout"; $my_attribute->setData("used_in_forms", $used_in_forms) ->setData("is_used_for_customer_segment", true) ->setData("is_system", 0) ->setData("is_user_defined", 1) ->setData("is_visible", 1) ->setData("sort_order", 100); $my_attribute->save();
2 – show custom attribute for customer :
after we added the custom attribute magento 2 show by defaut in in backend for $used_in_forms[]="adminhtml_customer" , but in frontend you need some additional code to use for example we want show in page customer register ($used_in_forms[]="customer_account_create"):
first inside your module go or create Ibnab/CustomerPut/view/frontend/layout and create new handler xml customer_account_create.xml and push :
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="form.additional.info"> <block class="Magento\Framework\View\Element\Template" name="my_form_additional_info_customer" template="Ibnab_CustomerPut::additionalinfocustomer.phtml"/> </referenceContainer> </body> </page>
Now create .phtml inside Ibnab/CustomerPut/view/frontend/templates (additionalinfocustomer.phtml) and push :
<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>"> <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend><br></p> <p> <div class="field my_attribute required"> <label for="my_attribute" class="label"><span><?php /* @escapeNotVerified */ echo __('My Attribute') ?></span></label> <div class="control"> <input type="text" name="my_attribute" id="my_attribute" title="<?php /* @escapeNotVerified */ echo __('My Attribute') ?>" class="input-text" data-validate="{required:true}" autocomplete="off"> </div> </div> </div> </fieldset>
Videos workshop :
Magento 2.1 for developers : add category eav attribute programmatically (magento2.1 and 2.07)
Magento 2 Kill EAV For Customers video courses Part 1
Magento 2 Kill EAV For Customers video courses Part 2
ok you can try to create accout your custom field will be showed ....
Comments