I need to add additional fields in the Account information section on My Account page.
In order to do that, where I have to add the code?
Any help will be appreciated!
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityTry the following code which creates Customattribute
attribute and added account information tab automatically.
For that, you need to create one file at Vendor\ModuleName\Setup\UpgradeData.php and paste below code and change attribute Name and code as per your need.
namespace Vendor\ModuleName\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use \Magento\Framework\Setup\UpgradeDataInterface;
use \Magento\Framework\Setup\ModuleContextInterface;
use \Magento\Framework\Setup\ModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
/**
* 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;
}
/**
* Upgrade data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$this->createCustomAttribute($setup);
}
}
public function createCustomAttribute($setup)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
// Add new customer attribute
$customerSetup->addAttribute(
Customer::ENTITY,
'customattribute',
[
'label' => 'Customattribute',
'input' => 'text',
'required' => false,
'sort_order' => 1000,
'position' => 1000,
'visible' => true,
'system' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'is_searchable_in_grid' => false,
'default' => '0'
]
);
// add attribute to form
/** @var $attribute */
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customattribute');
$attribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']);
$attribute->save();
$setup->endSetup();
}
}
Solution provided by Sohel Rana at this link. So, if it's working then you must thanks him.