Customer custom attribute

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Hi,
how can i check the value of a customer custom attribute?
say i created an attribute called <Is Preferred> and its type of dropdown with "yes" and "no" values.

how can i get the value of Is Preferred attribute for a signed in customer?

thanks


ps: is it possible to search in forum topics? cant seem to find a search feature.
7 years ago
ulker4hire wrote:
Hi,
how can i check the value of a customer custom attribute?
say i created an attribute called <Is Preferred> and its type of dropdown with "yes" and "no" values.

how can i get the value of Is Preferred attribute for a signed in customer?

thanks


ps: is it possible to search in forum topics? cant seem to find a search feature.


Hi,

You can use the GetAttribute extension method from GenericAttributeExtensions class. Here is an example:


string isPreferredGenericAttributeKey = "IsPreferred";
string isPreferred = customer.GetAttribute<string>(isPreferredGenericAttributeKey);


Hope this helps!

Regards,
Hristian
7 years ago
thanks for the reply.
so that attribute key is the name i pick with spaces moved?
if my custom attribute name is <Is Preferred Customer> the key is "IsPreferredCustomer", right?

thanks again
7 years ago
I think the code Hristian posted is actually just to retrieve a generic attribute associated with the customer. If you're talking about a Custom customer attribute that can be defined from the Customer form fields tab on the Customer Settings page then it's a bit different as multiple attributes are stored in an XML format.

The XML is still stored as a generic attribute so it starts the same:

var customAttributesXml = customer.GetAttribute<string>(SystemCustomerAttributeNames.CustomCustomerAttributes)

Which should return an XML representation of the customer's custom attributes which you'll need to parse to get the one you're interested in. I haven't tried it but I'd guess that you need one of the functions in the CustomerAttributeParser so possibly something like:

var isPreferredValue = ParseValues(customAttributesXml, {customerAttributeId}).FirstOrDefault()

Where {customerAttributeId} is the integer Id of the Custom customer attribute that you created in the admin site.
7 years ago
You are exactly right.

The only problem here is the Id of the attribute i want.
It is impossible to know the Id if there are many custom attributes.

Is there a better way to handle this?

thank you
7 years ago
ulker4hire wrote:
The only problem here is the Id of the attribute i want.
It is impossible to know the Id if there are many custom attributes.

Is there a better way to handle this?

Just edit the attribute from the admin site, it'll open a page with a url like this:
http://admin-demo.nopcommerce.com/Admin/CustomerAttribute/Edit/1

The number on the end of the url is the attribute's Id.

Alternatively you can look in the CustomerAttribute table in the database.
7 years ago
Hi,

does anyone know if there is a way for the "(optional)" after Phone number to be removed in the checkout stage?

Thanks,
Rachel.
7 years ago
RachelJ wrote:
does anyone know if there is a way for the "(optional)" after Phone number to be removed in the checkout stage?

All text can be edited from the string resources tab on the appropriate language page, just search for the value you want to change.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.