0

I have created a custom attribute for a customer and I can see it in the DB, inside the customer_eav_attribute table:

enter image description here

It was inserted using the following code:

/** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId(); 
    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create(); 
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

    $customerSetup->addAttribute(Customer::ENTITY, \Shreeji\CustomerApproval\Model\Config::CUSTOMER_APPROVE_ATTRIBUTE_CODE, [
        'type' => 'varchar',
        'label' => 'Is Approved',
        'input' => 'select',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'sort_order' => 10,
        'position' => 10,
        'system' => 0,
        'source' => 'Shreeji\CustomerApproval\Model\Config\Source\ActivationType',
        //'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
        'adminhtml_only'=>1

    ]);
    $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, \Shreeji\CustomerApproval\Model\Config::CUSTOMER_APPROVE_ATTRIBUTE_CODE)
    ->addData([
        'attribute_set_id' => $attributeSetId,
        'is_used_in_grid' => 1,
        'attribute_group_id' => $attributeGroupId,
        'is_visible_in_grid' => 1,
        'is_searchable_in_grid' => 1,
        'is_filterable_in_grid' => 1,
        'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address','adminhtml_customer', 'customer_address_edit', 'customer_register_address']
    ])->save();

Then, I have a method that gets executed when a user logs in.

public function execute(\Magento\Framework\Event\Observer $observer)
{
    if(!$this->_config->isActive()) {
        return $this;
    }
    $customer = $observer->getEvent()->getCustomer();
    $session = $this->_objectManager->get('Magento\Customer\Model\Session');
    $aprroveSatus=$this->_config->isApprovedCustomer($customer);
.....

and

public function isApprovedCustomer($customer){

    $approveStatus=$customer->getData();
    var_dump($approveStatus);
    die('here');
.......

When I login with a user that has a value set for that custom attribute (as visible in customer_entity_varchar): enter image description here .. I get this in return:

array (size=15)
  'entity_id' => string '11' (length=2)
  'group_id' => int 5
  'default_billing' => string '0' (length=1)
  'default_shipping' => string '0' (length=1)
  'created_at' => string '2017-08-01 13:50:29' (length=19)
  'updated_at' => string '2017-08-02 15:07:26' (length=19)
  'created_in' => string 'Maxtel BG' (length=9)
  'email' => string 'team@creatizmo.com' (length=18)
  'firstname' => string 'Stanimir' (length=8)
  'lastname' => string 'Genovov' (length=7)
  'store_id' => int 1
  'website_id' => int 1
  'addresses' => 
    array (size=0)
      empty
  'disable_auto_group_change' => int 0
  'attribute_set_id' => int 1
here

The custom attribute's name is si_is_approved I can't find out a way to obtain that value, when it is clearly set. getCustomAttributes returns an empty array and getData returns the array above. What is wrong with the custom field and why can't I obtain the value for it ?

2

It turned out to be an issue with the attribute set IDs. I used the migration tool and this mixed up the attribute sets, due to a bug in Magento: https://github.com/magento/magento2/issues/4677

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.