5

I created customer custom attribute with input multiselect like this:

$code = 'multiselect_test';
$insertData = array(
        "type"     => "text",
        "backend"  => "",
        "label"    => 'Multiselect Input',
        "input"    => 'multiselect',
        "source"   => "",
        "visible"  => true,
        "required" => false,
        "default" => "",
        "frontend" => "",
        "unique"     => false,
        "note"       => ""
      );
       $values = array('Briteny','James');
       $insertData['source'] = 'Magento\Eav\Model\Entity\Attribute\Source\Table';
       $insertData['backend'] = 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend';
       $insertData['option']['values'] = $values;


$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $code,  $insertData);
      $attribute   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);

      $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
      $used_in_forms[]="adminhtml_customer";
      $used_in_forms[]="checkout_register"; 
      $used_in_forms[]="customer_account_edit"; 
      $attribute->setData("used_in_forms", $used_in_forms)
      ->setData("is_used_for_customer_segment", true)
      ->setData("is_system", 0)
      ->setData("is_user_defined", 1) 
      ->setData("sort_order", $data['sorting_order']);

    try {
      $attribute->save();
    } catch (Exception $e) {
      $this->_logger->addError($e->getMEssage());
    }

i created a custom input for customer registration form in frontend for that custom attribute like this:

<select multiple name="multiselect_test">
  <option value="0">Britney</option>
  <option value="1">James</option>
</select>

when i looked in backend to edit the customer info in account information the custom attribute input value from frontend was not there, i guess it's because the value for this custom attribute is wrong (not 0 or 1), then i try to insert the value from backend and save it, and the input value save properly, when i try to retrieve the value like this:

$cattrValue = $customer->getCustomAttribute('multiselect_test')->getValue();

i got this result :

39,40

In conclusion i need to know how to retrieve this multiselect/dropdown custom attribute real value and label information, so i can use it to create custom attribute input for customer registration properly, or use it for other process in backend

2
  • try with, $customer->getCustomAttribute('multiselect_test')->getLabel(); – Rakesh Jesadiya Jul 6 '17 at 6:54
  • @RakeshJesadiya i want to retrieve each option from select input label, not the general label – simple guy Jul 6 '17 at 6:57
3

you can use eav atrribute interface to retrieve available options for your dropdown/multiselect like this:

public function __construct(
        \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository
    ) {
        $this->eavAttributeRepository = $eavAttributeRepository; 
    }

public function retrieveOptions($custom_attribute_code){
        $attributes = $this->eavAttributeRepository->get(\Magento\Customer\Model\Customer::ENTITY, $custom_attribute_code);
        $options = $attributes->getSource()->getAllOptions(false);
}
-1

Use the below code to get and set custom attribute value.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->getCollection();
    foreach ($customerObj as $customerObjdata) {

        $customermodel  = $objectManager->create('Magento\Customer\Model\Customer');
        $customerData = $customermodel->getDataModel();
        $customerData->setId($customerObjdata->getData('entity_id'));
        $customerData->setCustomAttribute('is_vendor', 0);
        $customermodel->updateData($customerData);

        $customerResource = $objectManager->create('\Magento\Customer\Model\ResourceModel\CustomerFactory')->create();
        $customerResource->saveAttribute($customermodel, 'is_vendor');
    }
1
  • Why not using dependency injection? – PY Yick Jul 6 '17 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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