Accessing Generic Attributes eg. a customer's gender

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Does anyone know how to access the generic attributes (i.e. those stored in the dbo.GenericAttribute table)?  

For example I'd like to get hold of the current user's gender in one of my views.  Is there a helper function/service for this?

I've tried something along the lines of...


var customer = WorkContext.CurrentCustomer;
var model = new CustomerInfoModel();
CustomerController.PrepareCustomerInfoModel(model, customer, false);

if (model.Gender == "M") {
// do stuff
}


but the PrepareCustomerInfoModel is a protected member function and so not accessible in this way (and creating a CustomerController object isn't possible in this way).

Any suggestions most appreciated.
Thanks,
William
11 years ago
var customer = WorkContext.CurrentCustomer;
var gender = customer.GetAttribute<string>(SystemCustomerAttributeNames.Gender);
11 years ago
Thanks very much Mariann - this is very useful!

Just in case you're interested, I've been using this to change the css based on user attributes (Head.cshtml grabs a different css file) e.g. if the user is male then the page is presented slightly differently. This will hopefully turn into a cool little feature where users can customise their views via their account settings (a bit like the theme selection option but more lightweight).

Anyway, thanks again!
William
11 years ago
Something similar, but as I'm "new" to nop I'm at the bottom of the learning curve.  So please forgive my ignorance.

I would like to add additional customer preferences such as preferred sizes.  Similar to Male/Female options, I would like to add Preferred Shoe Size: then have a drop down of sizes for the customer to select.  This would be saved along with the customer data (gender, Preferred Shoe size, preferred Shirt size, etc.).  

I'm not sure where to start on this?
10 years ago
I have made a post about this...

https://www.nopcommerce.com/boards/t/28080/add-new-properties-to-product-in-280.aspx#114011

and i was wondering if this is possible using generic attributes...and if its available in version 2.80?

S
6 years ago
Mariann wrote:
var customer = WorkContext.CurrentCustomer;
var gender = customer.GetAttribute<string>(SystemCustomerAttributeNames.Gender);


Hi,

Can you please tell me if we can also use the above logic to extract Current Customer's Reward point Balance. If Yes how.

Thanks in advance
4 years ago
Mariann wrote:
var customer = WorkContext.CurrentCustomer;
var gender = customer.GetAttribute<string>(SystemCustomerAttributeNames.Gender);


But it is saying:
Customer doesn't contain a definition for GetAttribute.
What I am missing? Using nopCommerce 4.20
4 years ago
In 4.2
                    model.Gender = _genericAttributeService.GetAttribute<string>(customer, NopCustomerDefaults.GenderAttribute);

Refer to nop4.2.0\Presentation\Nop.Web\Areas\Admin\Factories\CustomerModelFactory.cs
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.