Creating a Custom Registration Form with Contact Info and Company Details in Magento
Hello developers! Today I’m going to guide you through creating a custom registration form in Magento that includes contact information and company details. We’ll keep the default registration page intact while adding our new functionality.
Understanding the Default Registration Process
First, let’s understand how the default registration process works in Magento 2. The app\code\core\Mage\Customer\controllers/AccountController file contains the createPostAction() method responsible for processing customer registration:
public function createPostAction()
{
// ... (omitting initial code)
if ($this->getRequest()->isPost()) {
// ... (processing form data and validation)
}
}
This action handles both customer and address data extraction, validation, and saving. It saves the customer and address information into the customer_entity and customer_address_entity tables respectively.
Creating a New Registration Functionality
Since you don’t want to edit the existing function due to potential conflicts with core functionality, we’ll create a new controller action that will handle our custom registration process:
-
In your custom module (let’s call it
MyCompany_CustomRegistration), navigate to thecontrollersfolder and create anAccountController.phpfile. -
Add the following code to the
AccountController.phpfile:
namespace MyCompany\CustomRegistration\Controller\Account;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\AddressFactory;
use Magento\Customer\Model\CustomerFactory;
use Psr\Log\LoggerInterface;
class CustomCreate extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
{
protected $session;
protected $addressFactory;
protected $customerFactory;
protected $logger;
public function __construct(
\Magento\Framework\App\Action\Context $context,
Session $session,
AddressFactory $addressFactory,
CustomerFactory $customerFactory,
LoggerInterface $logger
) {
$this->session = $session;
$this->addressFactory = $addressFactory;
$this->customerFactory = $customerFactory;
$this->logger = $logger;
parent::__construct($context);
}
public function execute()
{
// ... (implement your custom registration logic here)
}
}
- In the
execute()method, implement your custom registration logic:
public function execute()
{
if ($this->getRequest()->isPost()) {
$customer = $this->customerFactory->create();
$address = $this->addressFactory->create();
// Extract and validate customer data from post request
try {
// Save the customer and address data to the database
$customer->save();
$address->setParentId($customer->getId())->save();
// ... (handle successful registration)
} catch (\Exception $e) {
$this->logger->critical($e);
// ... (handle exceptions)
}
}
return $this->resultPageFactory->create();
}
- In your custom module’s
di.xmlfile, add the following configuration to map your new action:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\Action\Wrapper">
<arguments>
<argument name="actions" xsi:type="array">
<item name="MyCompany_CustomRegistration/js_routers::customroute/customer/customcreate/*" xsi:type="string">MyCompany\CustomRegistration\Controller\Account\CustomCreate</item>
</argument>
</arguments>
</type>
</config>
-
Create a new form in your custom module’s
view/frontend/templatesfolder to display the custom registration form. -
In your theme, create or modify a layout XML file (e.g.,
app/design/frontend/{your_package}/{your_theme}/Magento_Theme/layout/default.xml) and add the following code:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:view/page_structure.xsd">
<body>
<!-- Add your custom registration block here -->
<referenceBlock name="custom.registration.block" template="MyCompany_CustomRegistration::register.phtml"/>
</body>
</page>
-
In your theme’s
app/design/frontend/{your_package}/{your_theme}/Magento_Theme/templatesfolder, create a file namedregister.phtmland design your custom registration form. -
Once the user submits the form, it will be processed by the
execute()method in our new controller action.
Handling Custom Form Submission
When the user submits the custom registration form on the frontend, the request will be routed to the configured URL for our customcreate action. The execute() method in our custom controller will then process and save the customer data as described earlier.
Remember that you’ll need to handle the successful registration process appropriately, such as displaying success messages or redirecting the user to a specific page.
Conclusion
In this tutorial, we’ve created a new registration functionality in Magento 2 while keeping the default registration process intact. By following these steps, you can now collect and store customer contact information and company details separately from the standard account creation process.
This approach allows you to maintain the core functionality of Magento while adding custom features tailored to your business requirements. Don’t forget to consult the official Magento Developer Documentation for more in-depth guidance on creating custom modules: https://laravelcompany.com/2023-magento-2-developer-documentation
Happy coding!