5

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?

4

2 に答える 2

5

チェックボックスの名前を追加する必要があります:

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

それからそれは動作します

于 2012-09-03T18:08:55.273 に答える
2

私はそれをjavascriptで動作させました。select 要素を非表示にして、jQuery でチェックボックスを追加します。ユーザーがチェックボックスをクリックすると、選択の値が変更されます。

(function($){
    $(document).ready(function(){
        var selects = $('.checkselect');
        $.each(selects, function(index, select){
            var checkbox = "<input class='selectcheckbox' type='checkbox' value='0' />";
            $(select).append($(checkbox));
            $(select).find('select').hide();
            $(select).on('click', '.selectcheckbox', function(){
                if($(this).is(':checked'))
                    $(select).find('select').val('1');
                else 
                    $(select).find('select').val('0');
            });
        });
    });
})(jQuery);

最善の解決策ではありませんが、プロジェクトを進めなければなりません。誰かがより良い解決策を見つけた場合は、ここで回答してください。

于 2012-07-27T05:08:20.747 に答える