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);
}
Tried to do with custom module.But its giving an error.
1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory.
The following modules are outdated:
Onjection_Customer data: current version - none, required version - 1.0.0...
Module Code
app/code/Onjection/Customer/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Onjection_Customer',
__DIR__
);
2.app/code/Onjection/Customer/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Onjection_Customer" setup_version="1.0.0">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
3.app/code/Onjection/Customer/Setup/InstallData.php
<?php
namespace Onjection\Customer\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/**
* @codeCoverageIgnore
*/
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();
}
}
Commands used for installation :
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
Step 1: Create a file called 'UpgradeSchema. php' in the setup folder of module, i.e. Step 2: After saving files, you need to run php bin/magento setup:upgrade. Now check your database and you will be able to find a new custom table and new column in 'sales_order_payment' table.
To add a new Magento 2 column to the order grid, you need to create and register a custom module. Then, create the Resource Model using the _renderFiltersBefore method. After this, you need to develop a UI component.
Create a column to product grid by creating a new product attribute 2. Result The most given task for all Magento programmers should be adding more columns with custom data to the Magento base grid. In this blog, we are going to learn how to add columns to the product grid in Magento 2.
Add a custom column in the customer grid Using the below way. ==> please create the first module and follow the below step ==> Now we used to the plugin, please create the file at the below location.
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
It is defined in Magenest\Customer\Setup\Patch\Data\AddCustomerAttribute.php 1. Create a table In order to show whichever data we want, we’ll store it in our own table. Here, we’ll simply call it magenest_custom_column with only two columns, id and value.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With