1

I am creating a module that will prevent the user from logging in until the admin has approved the account. I have added a custom attribute using the below install script:

<?php

$installer = $this;
$attribute  = array(
    'type' => 'int',
    'input' => 'text',
    'label' => 'Is Active',
    'global' => 1,
    'visible' => 1,
    'default' => '0',
    'required' => 0,
    'user_defined' => 0,
    'used_in_forms' => array(
        'adminhtml_customer',
    ),
    'comment' => 'Flag to check if user can log into their account. 0 = No, 1 = Yes.',
); 

$installer->addAttribute('customer', 'can_log_in', $attribute);
$installer->endSetup();

The account listener works and the attribute is being set (I can see it in the database). The problem is, I cannot see it in the admin area (Customers -> Manage Customers -> Edit -> Account Information) to toggle.

How do I get my attribute to appear in the Account Information segment of the edit customer form?

3 Answers 3

7

Try this

<?php

$installer = $this;
$attribute  = array(
    'type' => 'int',
    'input' => 'text',
    'label' => 'Is Active',
    'global' => 1,
    'visible' => 1,
    'default' => '0',
    'required' => 0,
    'user_defined' => 0,
    'used_in_forms' => array(
        'adminhtml_customer',
    ),
    'comment' => 'Flag to check if user can log into their account. 0 = No, 1 = Yes.',
); 

$installer->addAttribute('customer', 'can_log_in', $attribute);

 Mage::getSingleton('eav/config')
    ->getAttribute('customer', 'can_log_in')
    ->setData('used_in_forms', array('adminhtml_customer'))
    ->save();

$installer->endSetup();

if you want to add it in all the place use below

 Mage::getSingleton('eav/config')
        ->getAttribute('customer', 'can_log_in')
    ->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
    ->save();
5
  • This has worked. I am only interested in letting the Admin see/edit this value. Why does this work, and what I did not? Jan 13, 2016 at 11:23
  • if you check the entity table in database called "customer_form_attribute " you will find different form_codes. These are referring to different form attributes and for the latest magento you must require to add attribute to different form codes Jan 13, 2016 at 11:30
  • In my attribute array I have used_in_forms and set adminhtml_customer. Why did that not save? Why do we need to use the eav/config to set the data? Jan 13, 2016 at 11:48
  • Check my answer i had faced same problem when am working on it.
    – Sourav
    Jul 29, 2016 at 2:12
  • 1
    @RichardParnaby-King I encountered the same problem last year and only just worked it out. used_in_forms is not specified in Mage_Customer_Model_Resource_Setup::_prepareValues and so cannot work in addAttribute method, it must be added afterwards and then saved again. Jun 23, 2018 at 18:50
2

Try bellow script

<?php
$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer->startSetup();

$entityTypeId     = (int)$installer->getEntityTypeId('customer');
$attributeSetId   = (int)$installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = (int)$installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('customer', 'can_log_in', array(
    'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'Is Active',
    'forms'         => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
    'visible'       => 1,
    'required'      => 0,
    'user_defined'  => 1,
));

$installer->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, 'can_log_in', 100);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'can_log_in');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();

$installer->endSetup();
?>
0

custom attribute don't save value in database until you use system => 0; attribute in addAttribute() method.

Example:

$installer->addAttribute('customer', 'can_log_in', array( system => 0; ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.