3

I am using Magento 2.1.5, and created custom attribute for customer and trying to get & set the values of custom attribute but didn't get custom attribute in customer collection and as well as set also.

add custom attribute:

 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'sample_attribute');
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'sample_attribute',
        [
            'type' => 'int',
            'label' => 'Sample Attribute',
            'input' => 'select',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required' => true,
            'default' => '0',
            'sort_order' => 100,
            'system' => false,
            'position' => 100
        ]
    );
    $sampleAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'sample_attribute');
    $sampleAttribute->setData(
        'used_in_forms',
        ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
    );
    $sampleAttribute->save();

Get custom attribute value :

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerRepository = $objectManager->create('Magento\Customer\Model\Customer')->load('1');
    echo "<pre>"; print_r($customerRepository->getData());
  • I have updated code. Please let me know if you have query after using below code. – Rakesh Jesadiya Mar 31 '17 at 9:43
  • Rakesh, am facing same issue when am loading in controller & am getting 500 error – Ramesh Mar 31 '17 at 12:32
  • Have you remove var/generation folder?please check again i have updated answer. – Rakesh Jesadiya Mar 31 '17 at 12:35
  • Rakesh sorry to say, its generation issue . am able to get the getCustomAttribute. can't save the data with setCustomAttribute – Ramesh Mar 31 '17 at 12:54
3

You have to call below class in __consturct() function in your controller file and get and set value from customer.

  <?php
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    )
    {        
        $this->customerRepository = $customerRepository;
        parent::__construct($context);
    }

    public function getCustomerAttribute(){
        $customerId = 1;
        $customer = $this->customerRepository->getById($customerId);

        //get customer custom attribute value
        $customer->getCustomAttribute('sample_attribute');

        //set customer custom attribute value
        $sample_attribute = 2;
        $customer->setCustomAttribute('sample_attribute',$sample_attribute);
       try {                
            $customer = $this->customerRepository->save($customer);
        }catch (Exception $e) {
            return $e->getMessage();
        }
    }

Remove var/generation folder.clear cache.

|improve this answer|||||
  • I am trying to get at controller but \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, this is not working at contoller – Ramesh Mar 30 '17 at 14:03
  • It's working, but why can't use Magento\Customer\Model\ResourceModel\CustomerRepository with getById and save method's – Ramesh Mar 31 '17 at 13:02
  • can you please clarify my query ? – Ramesh Mar 31 '17 at 13:05
  • Sorry, I havent try with those method so i cant say more info, Use API method to save/update/get of customer object. its helpful for future with new major version released and if any update is made then its made inside model file so best way to use API method to get/set entity. – Rakesh Jesadiya Mar 31 '17 at 13:07
  • You can close this question so other users have useful in future. – Rakesh Jesadiya Mar 31 '17 at 13:20

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.