Sunday, May 21, 2017

How to add a custom column in customer table in Magento2?

Leave a Comment

I want to add a custom column(Telephone) in Customer table in magento2 and want to add value in this field while customer registration.

For this first I create a column(Telephone) in DB in customer_entity table.While create customer when I call $customer->setTelephone('1234567890') in Magento/Customer/Controller/Account/CreatePost.php in execute function. It is giving an error Undefine function setTelephone in Magento/Customer/Model/Data/Customer.php. But I already create this function in this Model.

Magento/Customer/Controller/Account/CreatePost.php

public function execute() {     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */     $resultRedirect = $this->resultRedirectFactory->create();     if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {         $resultRedirect->setPath('*/*/');         return $resultRedirect;     }      if (!$this->getRequest()->isPost()) {         $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]);         $resultRedirect->setUrl($this->_redirect->error($url));         return $resultRedirect;     }      $this->session->regenerateId();      try {         $address = $this->extractAddress();         $addresses = $address === null ? [] : [$address];          $customer = $this->customerExtractor->extract('customer_account_create', $this->_request);         $customer->setAddresses($addresses);          //Here is I set the telephone and it is giving an error         $customer->setTelephone('1234567890'); 

Magento/Customer/Model/Data/Customer.php

public function setTelephone($telephone) {     return $this->setData(self::TELEPHONE, $telephone); }  public function getTelephone() {     return $this->_get(self::TELEPHONE); } 

Magento/Customer/Api/Data/CustomerInterface.php

<?php namespace Magento\Customer\Api\Data;  interface CustomerInterface extends \Magento\Framework\Api\CustomAttributesDataInterface {     /* Add this code*/     const TELEPHONE = 'telephone';      public function getTelephone();      public function setTelephone($telephone);  }    

2 Answers

Answers 1

You need to create your own module

in Magento simple module example You can check the https://github.com/magento/magento2-samples/blob/master/sample-module-form-uicomponent/view/adminhtml/ui_component/sampleform_form.xml they have provided functional for adding a new field

<field name="color">     <argument name="data" xsi:type="array">         <item name="config" xsi:type="array">             <!--component constructor-->             <item name="component" xsi:type="string">Magento_SampleForm/js/form/element/color-select</item>             <!--main template for form field that renders elementTmpl as a child template-->             <item name="template" xsi:type="string">ui/form/field</item>             <!--customized form element template that will show colors-->             <item name="elementTmpl" xsi:type="string">Magento_SampleForm/form/element/color-select</item>             <item name="label" xsi:type="string">Autumn colors</item>             <item name="visible" xsi:type="boolean">true</item>             <item name="dataType" xsi:type="string">text</item>             <item name="formElement" xsi:type="string">input</item>             <item name="source" xsi:type="string">sampleform</item>         </item>     </argument> </field> 

in m2 there is no need to edit mysql rows directly or change core code, everything you could and suppose to rewrite. Read docs about general principles of working with magento 2

and as mentioned in comments if you need telephone field it's already implement

Answers 2

You can create custom customer Attribute Adding Customer Attribute

1) Create the Module file

<?xml version="1.0" encoding="UTF-8"?> <config  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">    <module name="Kalpesh_Mobile"  setup_version="1.0.0">        <sequence>                        <!--<module name="Kalpesh_Mobile"/>-->             <module name="Magento_Customer"/>         </sequence>       </module> </config>    class InstallData implements InstallDataInterface     {         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)         {                     $setup->startSetup();               /** @var CustomerSetup $customerSetup */             $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);              $customerSetup->addAttribute(Customer::ENTITY, 'mobile', [                 'type' => 'varchar',                 'label' => 'Mobile',                 'input' => 'text',                 'required' => false,                 'visible' => true,                 'user_defined' => true,                 'sort_order' => 1000,                 'position' => 1000,                 'system' => 0,             ]);              $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile')             ->addData([                 'attribute_set_id' => $attributeSetId,                 'attribute_group_id' => $attributeGroupId,                 'used_in_forms' => ['customer_address_edit'],             ]);              $attribute->save();                      $setup->endSetup();            }     } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment