Magento2: CustomAttribute not set when creating Customer with

Created on 22 Mar 2017  路  7Comments  路  Source: magento/magento2

Preconditions

  1. Magento 2.1.4
  2. Identified on PHP 7.0 on Zend Server 9.0.2

Steps to reproduce

  1. Create a Custom Attribute for a Customer: e.g. "tax_class_id"
  2. Have a Plugin that adds a CustomerRepository->beforeSave() function to intercept the Customer Repository save.
  3. On the frontend have an empty cart, add some Product and checkout using the default Magento Checkout
  4. On the Order Success Page select to create a Customer Account

Expected result

When trying to get the CustomAttribute "tax_class_id" inside the "beforeSave" interceptor by e.g. $customer->getCustomAttribute('tax_class_id') the return is an instance of Magento\Framework\Api\AttributeValue.

Actual result

Getting the Custom Attribute through e.g. $customer->getCustomAttribute('tax_class_id') inside the Plugin the "getCustomAttribute" return is NULL instead of the to be expected Magento\Framework\Api\AttributeValue instance.

So any direct OO invocation of $customer->getCustomAttribute('tax_class_id')->getValue() throws an exception. It is required to test for the existence of the Custom Attribute first and to initialize it through $customer->setCustomAttribute('tax_class_id', <some value>).

It seems that the Customer Data Model is not fully initialized with the Custom Attributes until it is first saved in the Repository. That sucks (like so many other things we slowly discover in endless hours of debugging the Magento Core because of customer feedback to our extensions).

Why is this? Why not simply return the AttributeValue instance with the default value defined for the Attribute? In Magento 1.9 the comparable approach worked, why not on Magento 2.1 anymore?

Call Stack for this example

screenshot_767

Clear Description Format is valid needs update

Most helpful comment

Could it be that you did not read this thread completely?

As stated in the original post the Magento release is 2.1.4 and for clarification CE.

And we are using an API function of the Magento Framework which is (as to Magento Documentation) the same as when using $customer->getData/->setData on an Custom Attribute that is assigned to the EAV Attribute Data Structure on Magento 1.9 for the Customer Data Tables.

We are saying that there is no correct initialization of the ->getCustomAttribute() function inside it, as compared to the equal function in Magento 1.9.

And the ->getCustomAttribute function is native functionality, so I do not understand where your thinking is going.

I think that it is not a hard issues to add Custom Attribute initialization to its default value in Magento 2, as it is implemented in Magento 1.9. And I do not see any argument why it is too hard to correctly duplicate a behavior from Magento 1.9 into 2.1 or above.

In these discussions with the Magento Team we miss a lot as Developers, as apparently now there is a strong thinking in playing with Architecture instead of providing solid functionality to Store Owners and Developers - as it was in times of Magento 1.

Currently we are telling our customers to stay away from Magento 2 as long as possible, and we are even throttling our own extension development, as we continuously come across deficiencies and unnecessary complexity we do not know from Magento 1. Costing us hours of work we either are not able to invoice to our customers, or our customers do not want to expense.

I hope that your team will get its heads straight some time soon.

All 7 comments

Wait till you put a default value into an attribute and have it clobber your first write!

@pisc-software thank you for your report.
Which Magento edition do you use? As I can see you mention custom customer attributes.
Customer custom attributes are available in Enterprise edition only.
If you use this one, please contact Magento support instead of posting your issue here.
If you use Community Edition please address your question at programming questions forum or Magento Stack Exchange. As it is not a native behavior of Magento CE and we can not treat it as an issue

Could it be that you did not read this thread completely?

As stated in the original post the Magento release is 2.1.4 and for clarification CE.

And we are using an API function of the Magento Framework which is (as to Magento Documentation) the same as when using $customer->getData/->setData on an Custom Attribute that is assigned to the EAV Attribute Data Structure on Magento 1.9 for the Customer Data Tables.

We are saying that there is no correct initialization of the ->getCustomAttribute() function inside it, as compared to the equal function in Magento 1.9.

And the ->getCustomAttribute function is native functionality, so I do not understand where your thinking is going.

I think that it is not a hard issues to add Custom Attribute initialization to its default value in Magento 2, as it is implemented in Magento 1.9. And I do not see any argument why it is too hard to correctly duplicate a behavior from Magento 1.9 into 2.1 or above.

In these discussions with the Magento Team we miss a lot as Developers, as apparently now there is a strong thinking in playing with Architecture instead of providing solid functionality to Store Owners and Developers - as it was in times of Magento 1.

Currently we are telling our customers to stay away from Magento 2 as long as possible, and we are even throttling our own extension development, as we continuously come across deficiencies and unnecessary complexity we do not know from Magento 1. Costing us hours of work we either are not able to invoice to our customers, or our customers do not want to expense.

I hope that your team will get its heads straight some time soon.

@pisc-software thank you for your bug report.
We cannot reproduce this issue as described. Please provide the detailed steps we must follow to reproduce this issue.
Please give an install script, that added customer custom attribute 'tax_class_id' and plugin codes.

@pisc-software we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.

@pisc-software, we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.

Getting the Custom Attribute through e.g. $customer->getCustomAttribute('tax_class_id') inside the Plugin the "getCustomAttribute" return is NULL instead of the to be expected Magento\Framework\ApiAttributeValue instance.

Please check the contract of \Magento\Framework\Api\CustomAttributesDataInterface::getCustomAttribute method. It may return null and your code should take it into account.

SImilar to the \Magento\Catalog\Model\Product::getExtensionAttributes logic - you may load necessary custom attributes by yourself.

Was this page helpful?
0 / 5 - 0 ratings