3

I have created a custom attribute for customer using below code.

setup/installData.php

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'samsung', [
        'type' => 'varchar',
        'label' => 'Samsung ',
        'input' => 'boolean',
        'source' => '',
        'required' => false,
        'visible' => true,
        'position' => 166,
        'system' => false,
        'backend' => ''
    ]);


    $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'samsung')
        ->addData(['used_in_forms' => [
            'adminhtml_customer',
            'adminhtml_checkout',
            'customer_account_create',
            'customer_account_edit'
        ]]);
    $attribute->save();

I am using below front-end code but data is not stored in the database:

<div class="field apple">
    <div class="control">
        <input type="checkbox" name="acs" id="apple" value="Apple" class="checkbox">apple
    </div>
    </div>
    <div class="field samsung">
    <div class="control">
        <input type="checkbox" name="acs" id="samsung" value="Samsung" class="checkbox">Samsung
    </div>
    </div>
    <div class="field sony">
    <div class="control">
        <input type="checkbox" name="acs" id="sony" value="Sony" class="checkbox">Sony
    </div>
    </div>
    <div class="field dell">
    <div class="control">
        <input type="checkbox" name="acs" id="dell" value="Dell" class="checkbox">Dell
    </div>
    </div>
    <input type="button" onclick='selectAll()' value="Select All"/>
    <input type="button" onclick='UnSelectAll()' value="Unselect All"/>

    <script type="text/javascript">
        function selectAll(){
            var items=document.getElementsByName('acs');
            for(var i=0; i<items.length; i++){
                if(items[i].type=='checkbox')
                    items[i].checked=true;
            }
        }

        function UnSelectAll(){
            var items=document.getElementsByName('acs');
            for(var i=0; i<items.length; i++){
                if(items[i].type=='checkbox')
                    items[i].checked=false;
            }
        }           
    </script>
1
  • Where is the code for saving? Write that too. Commented Feb 21, 2019 at 16:04

1 Answer 1

1

please use below code:

    <div class="field apple">
        <div class="control">
            <input type="checkbox" name="apple" id="apple" value="1" class="mycustomclass">Apple
        </div>
        </div>
        <div class="field samsung">
        <div class="control">
            <input type="checkbox" name="samsung" id="samsung" value="1" class="mycustomclass">Samsung
        </div>
        </div>
        <div class="field sony">
        <div class="control">
            <input type="checkbox" name="sony" id="sony" value="1" class="mycustomclass">Sony
        </div>
        </div>  
    <input type="button" onclick='selectAll()' value="Select All"/>
    <input type="button" onclick='UnSelectAll()' value="Unselect All"/>

    <script>
jQuery(document).ready(function() {
  jQuery("#btn_checkall").click(function() {
    jQuery("input.mycustomclass").each(function() {
      jQuery(this).prop("checked", true);
    });
  });
  jQuery("#btn_uncheckall").click(function() {
    jQuery("input.mycustomclass").each(function() {
      jQuery(this).prop("checked", false);
    });
  });
});
</script>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.