cancel
Showing results for 
Search instead for 
Did you mean: 

Setting Customer Attribute to null after previous value fails

Setting Customer Attribute to null after previous value fails

This error came up in 2.4.5

If a custom attribute is set to an object in this interface 

\Magento\Customer\Api\Data\CustomerInterface

And then in an instance of customer, set the custom attribute to a value.

$customer->setCustomAttribute('custom_customer_var', '2.4.5');
$custRepo->save($customer);

Following that (or at any time as required in an application), we would like to set that value to null.

$customer->setCustomAttribute('custom_customer_var', null);
$custRepo->save($customer);
$newCustRef = $custRepo->get($custRepo->getEmail()); // not listed here, but customer registry cache clean is done too
$this->assertNull($newCustRef->getCustomAttribute('custom_customer_var')->getValue()); // would be null if correct.
// Responds : Failed asserting that '2.4.5' is null.

There is also something that is not ideal, in that you need to check for the existence of :

$newCustRef->getCustomAttribute('custom_customer_var') before calling getValue(). This makes for messy / ugly code which ideally Magento should be handling under the covers.

 

2 REPLIES 2

Re: Setting Customer Attribute to null after previous value fails

Hi,
I'm having the same problem. I'm unable to update the custom attribute to null or empty string in customer_entity_varchar table. I've recently upgraded to 2.4.5-p1. It is breaking the exiting functionality. 

Re: Setting Customer Attribute to null after previous value fails

I think I should also add, that it's also impossible to remove a value even in the admin for an EAV attribute on a customer. Try to clear out the previous value in the admin, save the customer, and notice it won't disappear. Such a big oversight.

 

This also applies to any other EAV using object such as products.