Making the “Company” Field Required in Magento B2B
As a Magento developer specializing in B2B implementations for clients like Laravel Company, I often receive queries about customizing the checkout process to meet specific business requirements. One common request is to make certain fields, such as “Company,” mandatory during customer registration.
To achieve this, you’ll need to modify your Magento configuration and override core files. Here’s a step-by-step guide on how to make the “Company” field required for new B2B customers.
Step 1: Enable B2B Features
Before proceeding, ensure that you have enabled the necessary B2B features in your Magento installation. This includes enabling the B2B module and configuring the company roles and attributes.
To do this:
- Navigate to Stores > Settings > Configuration > Customers > Customer Configuration.
- Under the Company Attributes section, make sure the Enable Company Attribute field is set to Yes.
- Save the configuration.
Step 2: Override the Core Registration Form
The next step involves overriding the core registration form template to include validation for the “Company” field. You’ll need to create a custom module and override the customer_account_create layout XML file.
-
Create a new directory under your Magento app directory, following the structure
app/code/Vendor/Module/etc/frontend/layout. Replace “Vendor” with your actual vendor name and “Module” with a unique name for this functionality. -
Inside the
layoutfolder, create a new XML file namedcustomer_account_create.xml. -
Add the following content to the
customer_account_create.xmlfile:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_form_register">
<action method="setTemplate">
<argument name="template" xsi:type="string">Magento_Customer::form/register.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
This XML file tells Magento to use the default register.phtml template for the customer registration form. This is a necessary step before applying our custom validation.
Step 3: Add Validation to the Registration Form
Now, let’s add the required validation for the “Company” field in your custom module.
-
Create a new directory under your Magento app directory following the structure
app/code/Vendor/Module/view/frontend/templates. -
Inside the
templatesfolder, create a new PHP file namedregister.phtml. -
Add the following content to the
register.phtmlfile:
<?php
/** @var \Magento\Customer\Model\ResourceModel\CollectionFactory $collectionFactory */
$collectionFactory = $block->getLayout()->createBlock('Magento\Framework\View\Element\Template')
->setTemplate('Magento_Customer::form/register.phtml');
/** @var \Magento\Framework\View\Element\Template $_template */
$_template = $collectionFactory->getChild('register');
This code initializes the registration template and allows us to override it in our custom module.
- Now, update the
register.phtmlfile with the following content:
<?php
/** @var \Magento\Customer\Model\ResourceModel\CollectionFactory $collectionFactory */
$collectionFactory = $block->getLayout()->createBlock('Magento\Framework\View\Element\Template')
->setTemplate('Vendor_Module::form/register.phtml');
/** @var \Magento\Framework\View\Element\Template $_template */
$_template = $collectionFactory->getChild('register');
echo $_template->toHtml();
This code overrides the registration template with our custom version located at app/code/Vendor/Module/view/frontend/templates/form/register.phtml.
Step 4: Add Required Validation to the Custom Register Template
Finally, let’s add the required validation for the “Company” field in your custom register template.
-
Create a new directory under your Magento app directory following the structure
app/code/Vendor/Module/view/frontend/templates/form. -
Inside the
formfolder, create a new PHTML file namedregister.phtml. -
Add the following content to the
register.phtmlfile:
<form id="form-validate" action="<?php /* @escapeNotVerified */ echo $block->getFormUrl() ?>" method="post"
enctype="multipart/form-data" class="form-create-account">
<fieldset>
<legend><?php /* @escapeNotVerified */ echo __('Create Account') ?></legend><br>
<?php if ($block->isTrackingAvailable()): ?>
<input type="hidden" name="session_redirect_url"
value="<?php /* @escapeNotVerified */ echo $block->getRedirectUrl() ?>">
<?php endif; ?>
<p class="required">
<label for="company"><?= __('Company') ?></label>
<div class="field">
<input type="text" id="company" name="company" data-validate="{required:true}"
data-ui-id="customer-form-register-company"
value="<?php /* @escapeNotVerified */ echo $block->getCustomer()->getCompany() ?>"/>
</div>
</p>
<!-- Include other form fields here, such as email and password -->
</fieldset>
</form>
This code adds the “Company” field to the registration form with data-validate="{required:true}". This ensures that the “Company” field is required when submitting the form.
Step 5: Compile Your Magento Store
After making these changes, it’s important to compile your Magento store for the modifications to take effect. You can do this by running the following command in your terminal:
php bin/magento setup:di:compile
Conclusion
Making the “Company” field required during customer registration in a B2B Magento site involves overriding core files and adding validation to the registration form template. By following the steps outlined above, you can ensure that new customers are required to enter their company information before completing the registration process.
Remember that customizing your Magento installation requires careful planning and testing to avoid any potential conflicts or issues. If you’re unsure about any part of this process, consider consulting with a professional Magento developer at Laravel Company.
With proper implementation and regular maintenance, your B2B Magento site can provide a seamless checkout experience for both customers and administrators alike.