Adding a required unchecked check box for Terms and Conditions on new account registration
-
13-01-2021 - |
Question
I'm trying to add a Checkbox to account registration for terms and conditions mainly due to GDPR.
I've looked at this question: magento 2 - Terms and conditions checkbox on register page
But the answer is fairly vague. Where in the register.phtml document must they go? I've tried several locations and it causes the register form to disappear or miss elements.
The checkbox needs to be a requirement for registration but must not already be checked.
I hope someone can help me.
Solution
Try following way:
Step 1: VendorName/ModuleName/view/frontend/layout/customer_account_create.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Framework\View\Element\Template" name="tc" template="VendorName_ModuleName::tc.phtml"/>
</referenceContainer>
</body>
</page>
Step 2: VendorName/ModuleName/view/frontend/templates/tc.phtml
<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:true}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom T&C') ?>
</label>
</div>
</div>
OTHER TIPS
The accepted answer works, but only makes sense if you want to really create your own code.
If you want to use magento native agreements that are also used in checkout, do the following:
Vendor_theme lives here: app/design/frontend/
Vendor_theme/Magento_Customer/layout/customer_account_create.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\CheckoutAgreements\Block\Agreements" name="register.agreements" as="agreements" template="Magento_CheckoutAgreements::additional_agreements.phtml"/>
</referenceContainer>
</body>
</page>
this way the checkout agreements get used for the customer register.