When we create a customer custom attribute from admin and we add a custom attribute in the frontend form, after form submission if any error would occur and the form would redirect to the same page, the form field values should be pre-filled in the same form, but the problem is that the customer custom attributes are not pre-filled in the same form. In this blog, we describing for the solution of this problem. In this case, we can use a customer session as a solution. Let’s take an example, our custom attribute is company_type created from Admin > Stores > Attributes > Customer > Add Attribute and we are displaying the company_type attribute in the registration form as you can see in the following screenshots:
Create a plugin for Magento\Customer\Controller\Account\CreatePost
File Path: app/code/Mage2/CompanyType/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Controller\Account\CreatePost">
<plugin name="mage2_company_type_selected"
type="Mage2\CompanyType\Plugin\StoreDataInSession" />
</type>
</config>
Declare a plugin class to set value in a session variable
Here we are creating a session variable register_form_data. To set this session variable, setRegisterFormData($params) method is used.
File Path: app/code/Mage2/CompanyType/Plugin/StoreDataInSession.php
<?php
declare(strict_types=1);
namespace Mage2/CompanyType/Plugin;
use Magento\Customer\Controller\Account\CreatePost;
use Magento\Customer\Model\Session;
class StoreDataInSession
{
/**
* @var Session
*/
protected Session $session;
/**
* @param Session $customerSession
*/
public function __construct(
Session $customerSession
) {
$this->session = $customerSession;
}
/**
* @param CreatePost $subject
* @return bool
*/
public function beforeExecute(CreatePost $subject): bool
{
$params = $this->request->getParams();
if ($params) {
$this->session->setRegisterFormData($params);
}
return true;
}
}
Create a helper to get session variable data
File Path: app/code/Mage2/CompanyType/Helper/CustomerInfo.php
<?php
declare(strict_types=1);
namespace Mage2\CompanyType\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Customer\Model\Session;
class CustomerInfo extends AbstractHelper
{
/**
* @var Session
*/
protected $customerSession;
/**
* @param Context $context
* @param Session $customerSession
*/
public function __construct(
Context $context,
Session $customerSession
) {
parent::__construct($context);
$this->customerSession = $customerSession;
}
/**
* @return array
*/
public function getCustomerSessionFormData()
{
return $this->customerSession->getRegisterFormData();
}
}
Override a template file to make the field selected
In this example, the company_type attribute type is Dropdown, that’s why we are overriding select.phtml file. As per your attribute type, you need to override accordingly phtml file. The available attribute type files are boolean, date, file, image, multiline, multiselect, select, text, and textarea.
File Path: app/design/frontend/<vendor>/<theme_name>/Magento_CustomerCustomAttributes/templates/form/renderer/select.phtml
<?php
/**
* Create account form template
*/
/* @var $block \Magento\CustomAttributeManagement\Block\Form\Renderer\Select */
?>
<?php
$helper = $this->helper('\Mage2\CompanyType\Helper\CustomerInfo');
$formData = $helper->getCustomerSessionFormData();
$fieldCssClass = 'field field-' . $block->getHtmlId();
$fieldCssClass .= $block->isRequired() ? ' required' : '';
?>
<div class="<?= /* @noEscape */ $fieldCssClass ?>">
<label class="label" for="<?= $block->getHtmlId() ?>"><span><?= $block->escapeHtml($block->getLabel()) ?></span></label>
<div class="control">
<select id="<?= $block->getHtmlId() ?>" name="<?= $block->escapeHtmlAttr($block->getFieldName()) ?>"<?php if ($block->getHtmlClass()) :
?> class="select <?= $block->getHtmlClass() ?>"<?php endif;?>>
<?php foreach ($block->getOptions() as $option) :?>
<?php $optValue = $block->getValue(); ?>
<?php if ($block->getHtmlId() == 'company_type' && $formData) : ?>
<?php $optValue = ($formData['company_type']) ?? $block->getValue(); ?>
<?php endif; ?>
<option value="<?= $block->escapeHtmlAttr($option['value']) ?>"<?php if ($option['value'] == $optValue) :
?> selected="selected"<?php endif;?>><?= $block->escapeHtml($option['label']) ?></option>
<?php endforeach;?>
</select>
<?php if ($_message = $block->getAdditionalDescription()) : ?>
<div class="note"><?= /* @noEscape */ $_message ?></div>
<?php endif; ?>
</div>
</div>
Create an event observer to unset the session
Once the form has been submitted successfully, you can unset the session variable (it is optional). In this example, we are displaying a custom attribute in the registration form, that’s why we are unsetting the session variable after successful registration.
File Path: app/code/Mage2/CompanyType/etc/frontend/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_register_success">
<observer name="custom_redirect_register_success" instance="Mage2\CompanyType\Observer\Register"/>
</event>
</config>
Declare an event observer class
Here to unset a session variable, unsRegisterFormData() method is used.
File Path: app/code/Mage2/CompanyType/Observer/Register.php
<?php
declare(strict_types=1);
namespace Mage2\CompanyType\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;
class Register implements ObserverInterface
{
/**
* @var CustomerSession
*/
protected $customerSession;
/**
* @param CustomerSession $customerSession
*/
public function __construct(
Session $customerSession
) {
$this->customerSession = $customerSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$this->customerSession->unsRegisterFormData();
}
}
We hope this blog may be understandable and useful to you. You can email us at mage2developer@gmail.com if we missed anything or want to add any suggestions. We will respond to you as soon as possible. Happy to help 🙂



