Skip to content

How to add customer Attribute inside custom tab section insted of “Account Information” in magento 2 customer attribute

I have created setup patch data using the attribute please check the below code and block using add custom tab

moduleDataSetup = $moduleDataSetup;
$this->customerSetupFactory = $customerSetupFactory;
$this->eavConfig = $eavConfig;
}

public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();

$customerSetup = $this->customerSetupFactory->create([‘setup’ => $this->moduleDataSetup]);

// Add global_customer_price attribute
$customerSetup->addAttribute(Customer::ENTITY, ‘global_customer_price’, [
‘type’ => ‘varchar’,
‘label’ => ‘Global Customer Price’,
‘input’ => ‘text’,
‘required’ => false,
‘visible’ => false,
‘user_defined’ => true,
‘position’ => 999,
]);

// Add global_customer_special_price attribute
$customerSetup->addAttribute(Customer::ENTITY, ‘global_customer_special_price’, [
‘type’ => ‘varchar’,
‘label’ => ‘Global Customer Special Price’,
‘input’ => ‘text’,
‘required’ => false,
‘visible’ => false,
‘user_defined’ => true,
‘position’ => 1000,
]);

// Add attributes to forms
$attributes = [‘global_customer_price’, ‘global_customer_special_price’];
foreach ($attributes as $attributeCode) {
$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $attributeCode);
$attribute->setData(‘used_in_forms’, [‘customerpriceform_’]);
$attribute->save();
}

$this->moduleDataSetup->getConnection()->endSetup();
}

public static function getDependencies()
{
return [];
}

public function getAliases()
{
return [];
}
}

_coreRegistry = $registry;
$this->_systemStore = $systemStore;
parent::__construct($context, $registry, $formFactory, $data);
}

/**
* @return string|null
*/
public function getCustomerId()
{
return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
}

/**
* @return MagentoFrameworkPhrase
*/
public function getTabLabel()
{
return __(‘Customer Price’);
}

/**
* @return MagentoFrameworkPhrase
*/
public function getTabTitle()
{
return __(‘Customer Price’);
}

/**
* @return bool
*/
public function canShowTab()
{
if ($this->getCustomerId()) {
return true;
}
return false;
}

/**
* @return bool
*/
public function isHidden()
{
if ($this->getCustomerId()) {
return false;
}
return true;
}

/**
* Tab class getter
*
* @return string
*/
public function getTabClass()
{
return ”;
}

/**
* Return URL link to Tab content
*
* @return string
*/
public function getTabUrl()
{
return ”;
}

/**
* Tab should be loaded trough Ajax call
*
* @return bool
*/
public function isAjaxLoaded()
{
return false;
}

public function initForm()
{
if (!$this->canShowTab()) {
return $this;
}
/**@var MagentoFrameworkDataForm $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix(‘customerpriceform_’);

$fieldset = $form->addFieldset(‘base_fieldset’, [‘legend’ => __(‘Customer Price’)]);
/*$fieldset->addField(
‘global_customer_price’,
‘text’,
[
‘name’ => ‘global_customer_price’,
‘data-form-part’ => $this->getData(‘target_form’),
‘label’ => __(‘Global Customer Price’),
‘title’ => __(‘Global Customer Price’),
‘note’ =>__(‘This price sets the price for all your products for this customer. Examples:
±10.99 – increase/decrease current price by given value
±15% – increase/decrease current price by given percent’)
]
);
$fieldset->addField(
‘global_customer_special_price’,
‘text’,
[
‘name’ => ‘global_customer_special_price’,
‘data-form-part’ => $this->getData(‘target_form’),
‘label’ => __(‘Global Customer Special Price’),
‘title’ => __(‘Global Customer Special Price’),
‘note’ => __(‘This price sets the special price for all your products for this customer. Examples:
±10.99 – increase/decrease special price by given value
±15% – increase/decrease special price by given percent’)
]
);*/
$this->setForm($form);
return $this;
}

/**
* @return string
*/
protected function _toHtml()
{
if ($this->canShowTab()) {
$this->initForm();
return parent::_toHtml();
} else {
return ”;
}
}
}