1

i'm working on a 2.1.9 magento2 CE installation,

i've added a custom attribute to the customer, everything it's working OK, but when the attribute is not set and I try to retrieve it with the code

$customer->getCustomAttributes()["custom_field"]->getValue() I get an exception, instead a null value.

if the attribute is set on the customer this method will retur the correct data.

the exception is this one:

Exception #0 (Exception): Notice: Undefined index: custom_field in /var/www/magento/[INSTALLATION_DIR]/app/design/frontend/[TEMPLATE_DIR]/Magento_Customer/templates/form/edit.phtml on line 33

Exception #0 (Exception): Notice: Undefined index: custom_field in /var/www/magento/[INSTALLATION_DIR]/app/design/frontend/[TEMPLATE_DIR]/Magento_Customer/templates/form/edit.phtml on line 33

in phtml files right now i'm working with a try catch block to avoid the exception, but when I'm working on a plugin the exception will stop the execution even in a try catch block.

any suggestion to check if custom attribute exist?

thanks

2

Templates are not responsible for handle any logic, so any try catch block inside of template is a blasphemy.

Put your logic in block. I imagine you can do something like:

public function getCustomfield()
{
    return !empty($customer->getCustomAttributes()["custom_field"]) ? $customer->getCustomAttributes()["custom_field"]->getValue() : '';
}

Then call it in your template.

| improve this answer | |
  • This is not an answer to the question. The Notice: Undefined index has nothing to do with the code being executed in a template. – naitsirch Apr 29 at 11:54
0

Use below code in phtml:

<?php echo $block->escapeHtml($_helper->getCustomerAttributeValue($this->getCustomer()->getId(), 'custom_field')); ?>

Helper.php

/**
 * {@inheritdoc}
 */
public function getCustomerAttributeValue($customerId, $attributeCode)
{
    $customerObject = $this->_customerFactory->create()->load($customerId);
    return $customerObject->getData($attributeCode);
}
| improve this answer | |
0

When I had a similar issue the reason was a wrong configuration of the attribute.

I created the attribute in an UpgradeData script like this:

$this->customerSetup->addAttribute(Customer::ENTITY, 'blacklisted', [
    'label' => 'Customer is blacklisted',
    'type' => 'int',
    'input' => 'boolean',
    'visible' => true,
    'required' => false,
    'user_defined' => true,
    'default' => 0,
    'is_used_in_grid' => true,
]);

The problem is that customer attributes have the system option set to true per default. This prevents the attribute from being added to the custom_attributes array in the customer's data. You can find more details about that here.

What I had to do was adding 'system' => false to the option array.

After that don't forget to clear the cache.

php bin/magento cache:clean
| improve this answer | |

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.