Question

I needed to create two new customer attributes. I have followed the guide from: http://www.fontis.com.au/blog/magento/know-more-about-your-customers-adding-custom-signup-attributes

to create these attributes. I have checked the 'eav_attribute' table in the database and can confirm that both attribute exists. The only thing was that I didn't know how to create a checkbox attribute so I have created both attributes as 'Yes/No'.

from following the code on how to display the field on the registration form I have done:

<li class="control">
<div class="input-box">
    <label for="publisheroffer"><?php echo $this->__('Publisher Offer') ?><span class="required">*</span></label><br />
    <input type="checkbox" name="publisheroffer" id="publisheroffer" value="<?php echo $this->htmlEscape($this->getFormData()->getPublisheroffer()) ?>" title="<?php echo $this->__('Publisher Offer') ?>" class="input-text" />
</div>
</li>

Where the attribute id is 'publisheroffer'. When the account is created it creates fine but the custom attribute fields don't change.

How do I display this attribute as a checkbox on the registration page, and how to process the values.

Thanks for all the help in advance.

Was it helpful?

Solution

To allow to publish an attribute in the register form and some others form page, you have to set that the attribute(s) is/are available to those forms.

To do that here is a sample code to put in your sql setup below. You can find the rest of the code I used for my Magento Username module on my github account.

/* @var $installer Diglin_Username_Model_Entity_Setup */
$installer = $this;

/* @var $eavConfig Mage_Eav_Model_Config */
$eavConfig = Mage::getSingleton('eav/config');

$store = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$attributes = $installer->getAdditionalAttributes();

foreach ($attributes as $attributeCode => $data) {
    $installer->addAttribute('customer', $attributeCode, $data);

    $attribute = $eavConfig->getAttribute('customer', $attributeCode);
    $attribute->setWebsite( (($store->getWebsite()) ? $store->getWebsite() : 0));

    if (false === ($attribute->getIsSystem() == 1 && $attribute->getIsVisible() == 0)) {
        $usedInForms = array(
            'customer_account_create',
            'customer_account_edit',
            'checkout_register',
        );
        if (!empty($data['adminhtml_only'])) {
            $usedInForms = array('adminhtml_customer');
        } else {
            $usedInForms[] = 'adminhtml_customer';
        }
        if (!empty($data['adminhtml_checkout'])) {
            $usedInForms[] = 'adminhtml_checkout';
        }

        $attribute->setData('used_in_forms', $usedInForms);
    }
    $attribute->save();
}

OTHER TIPS

You can try the following code to create a checkbox custom attribute.

$customerSetup->addAttribute(Customer::ENTITY, 'customer_approved', [
            'type' => 'int',
            'label' => 'Customer Approved',
            'input' => 'boolean',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1001,
            'position' => 1001,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_approved')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();

Use the input 'boolean' instead of 'checkbox'.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top