How do you add a checkbox to the contact form and registration form in Magento 1.9, for example to confirm that the user is of legal age or to require acceptance of the privacy policy? A practical solution for this is the Terms and Conditions (Admin->Sales->Terms and Conditions). We now want to add confirmation of the privacy policy to the contact form and confirmation of the privacy policy and confirmation of legal age to the registration form, both in German and English (2 store views). To do this, we create 4 Terms and Conditions in the Magento backend (Age and Privacy Policy, each in German and English).
To retrieve the Terms and Conditions, we use the Checkout/Agreement model (see also https://stackoverflow.com/questions/24516943/get-the-content-of-terms-and-conditions-by-url):
if (Mage::getStoreConfigFlag('checkout/options/enable_agreements')) {
$agreements = Mage::getModel('checkout/agreement')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1);
}
If this is successful, we can base the subsequent output on the way the Terms and Conditions are displayed during checkout. See
\app\design\frontend\base\default\template\checkout\onepage\agreements.phtml
Adapted for the contact form, we now extend the ul list with additional li elements in the file (the package and template can of course be customized as desired)
\app\design\frontend\econcess\default\template\contacts\form.phtml
for example, in the following form:
if ($agreements) {
foreach ($agreements as $_a):
// ID 5 = Datenschutz-Agreement deutsch, ID 6 = Alter-18-Agreement englisch, ID 7 = Datenschutz-Agreement deutsch, ID 8 = Alter-18-Agreement englisch
if ($_a->getId() >= 7 && $_a->getId() <= 8) {
?>
<li>
<div class="checkout-agreements">
<div class="agreement-content"<?php echo ($_a->getContentHeight() ? ' style="height:' . $_a->getContentHeight() . '"' : '')?>>
<?php if ($_a->getIsHtml()):?>
<?php echo $_a->getContent() ?>
<?php else:?>
<?php echo nl2br($this->escapeHtml($_a->getContent())) ?>
<?php endif; ?>
</div>
<p class="agree">
<input type="checkbox" id="agreement-<?php echo $_a->getId()?>" name="agreement[<?php echo $_a->getId()?>]" value="1" title="<?php echo $this->escapeHtml($_a->getCheckboxText()) ?>" class="required-entry checkbox" /><label for="agreement-<?php echo $_a->getId()?>"><?php echo $_a->getIsHtml() ? $_a->getCheckboxText() : $this->escapeHtml($_a->getCheckboxText()) ?></label>
</p>
</div>
</li>
<?php
}
endforeach;
}
The IDs can of course be customized as desired.
This means we are already finished with the contact form, and we now apply this solution in a slightly modified form to the registration form as well, using the file (package and template can of course be customized as desired)
\app\design\frontend\econcess\default\template\persistent\customer\form\register.phtml
Here, we again extend the ul list at a suitable point, for example with (see above)
if (Mage::getStoreConfigFlag('checkout/options/enable_agreements')) {
$agreements = Mage::getModel('checkout/agreement')->getCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addFieldToFilter('is_active', 1);
}
if ($agreements) {
foreach ($agreements as $_a):
// ID 5 = Datenschutz-Agreement deutsch, ID 6 = Alter-18-Agreement englisch, ID 7 = Datenschutz-Agreement deutsch, ID 8 = Alter-18-Agreement englisch
if ($_a->getId() >= 5 && $_a->getId() <= 8) {
?>
<li>
<div class="checkout-agreements">
<div class="agreement-content"<?php echo ($_a->getContentHeight() ? ' style="height:' . $_a->getContentHeight() . '"' : '')?>>
<?php if ($_a->getIsHtml()):?>
<?php echo $_a->getContent() ?>
<?php else:?>
<?php echo nl2br($this->escapeHtml($_a->getContent())) ?>
<?php endif; ?>
</div>
<p class="agree">
<input type="checkbox" id="agreement-<?php echo $_a->getId()?>" name="agreement[<?php echo $_a->getId()?>]" value="1" title="<?php echo $this->escapeHtml($_a->getCheckboxText()) ?>" class="required-entry checkbox" /><label for="agreement-<?php echo $_a->getId()?>"><?php echo $_a->getIsHtml() ? $_a->getCheckboxText() : $this->escapeHtml($_a->getCheckboxText()) ?></label>
</p>
</div>
</li>
<?php
}
endforeach;
}
This gives the contact form one checkbox and the registration form two checkboxes, including the text field, and we are done. Tested with Magento 1.9.1.0.
