Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom checkbox field to customer in magento?

Tags:

magento

In mysql4-install-0.1.0.php, I can add custom textfield in my magento module like this:

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

$setup->addAttribute('customer', 'resale', array(
    'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'Resale number',
    'visible'       => 1,
    'required'      => 1,
    'user_defined' => 1,
));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'resale',
 '999'  //sort_order
);

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

I want also add a checkbox field. I add it like this:

$setup->addAttribute('customer', 'marketattended1', array(
    'input'         => 'checkbox',
    'type'          => 'int',
    'label'         => 'San Francisco International Gift Fair',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'marketattended1',
 '999'  //sort_order
);

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

I can see my checkbox field in admin/customer but when I'm trying to edit or add new customer it won't save the customer. It's just showing the "Please wait" indicator forever. How to make this work?

*Edit

Call to a member function setAttribute() on a non-object

I found this error or server response.

*Edit

I changed the installer code:

$setup->addAttribute('customer', 'marketattended1', array(
    'input'         => 'boolean',
    'type'          => 'int',
    'label'         => 'San Francisco International Gift Fair',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
    //'source'        => 'eav/entity_attribute_source_boolean'
));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'marketattended1',
 '999'  //sort_order
);

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

Now i can see select component with yes|no options and its working perfectly. It's also showing on customer registration form like this:

<div class="field">
   <label for="marketattended1">San Francisco International Gift Fair</label>
   <select id="marketattended1" name="marketattended1" class=" select">
      <option value="0">No</option>
      <option value="1">Yes</option>
   </select></div>

I want it to be checkbox. I have tried like this:

   <div class="field">
      <input class="checkbox" id="marketattended1" onchange="[removed]changechecked()" type="checkbox" value="1" />
      <label  class="required">*Others</label >
   </div>

But it won't save. How to make it save?

like image 907
Joonas Avatar asked Jul 24 '12 04:07

Joonas


1 Answers

you have to add the name of the checkbox:

<input class="checkbox" id="marketattended1" name="marketattended1" onchange="[removed]changechecked()" type="checkbox" value="1" />

Then it works

like image 57
Sebastian Avatar answered Nov 09 '22 23:11

Sebastian