How to use Nop_CustomerAttribute table

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hello...

I want to add a DNI field for a customer. Can I use Nop_CustomerAttribute table to store that information? where can I call it? Can you recommend me the best way to handle this in order to be able to upgrade nopCommerce in the future?

Thanks in advance
Jaime
13 years ago
Yes you can store this in the Nop_CustomerAttribute table. However this requires you to extend the code base becuase customer attributes are not configurable, they are currently hard coded.

1. Create a new class under Libraries/Nop.BusinessLogic/Customer called Customer.<your project name>.cs
2. Make the class partial and make sure the namespace is identical to that in Customer.cs.
3. Add a new property to your partial class like below.
4. All thats left after that is to make the necessary code changes to hook it up to the various UI's you want.

        public string DNI
        {
            get
            {
                var customerAttributes = this.CustomerAttributes;
                CustomerAttribute firstNameAttr = customerAttributes.FindAttribute("DNI", this.CustomerId);
                if (firstNameAttr != null)
                    return firstNameAttr.Value;
                else
                    return string.Empty;
            }
            set
            {
                if (value == null)
                    value = string.Empty;
                value = value.Trim();

                var customerAttributes = this.CustomerAttributes;
                CustomerAttribute firstNameAttr = customerAttributes.FindAttribute("DNI", this.CustomerId);
                if (firstNameAttr != null)
                {
                    firstNameAttr.Value = value;
                    IoC.Resolve<ICustomerService>().UpdateCustomerAttribute(firstNameAttr);
                }
                else
                {
                    firstNameAttr = new CustomerAttribute()
                    {
                        CustomerId = this.CustomerId,
                        Key = "DNI",
                        Value = value
                    };
                    IoC.Resolve<ICustomerService>().InsertCustomerAttribute(firstNameAttr);
                }
                ResetCachedValues();
            }
        }
12 years ago
public partial class CustomerAttribute : Customer ?
12 years ago
biapar wrote:
public partial class CustomerAttribute : Customer ?


The code would be like this:

namespace NopSolutions.NopCommerce.BusinessLogic.CustomerManagement
{
    public partial class Customer : BaseEntity
    {
        //degree451 code here
    }
}


As a good practice name the file something like MyAttributes.Customer.cs and place it in the same directory as Customer.cs
12 years ago
Which file , where i put the code. Thanks
12 years ago
I'm using 1.90

Really can't figure the picture

ok this

namespace NopSolutions.NopCommerce.BusinessLogic.CustomerManagement
{
    public partial class Customer : BaseEntity
    {
        
        public string DNI
        {
            get
            {
                var customerAttributes = this.CustomerAttributes;
                CustomerAttribute firstNameAttr = customerAttributes.FindAttribute("DNI", this.CustomerId);
                if (firstNameAttr != null)
                    return firstNameAttr.Value;
                else
                    return string.Empty;
            }
            set
            {
                if (value == null)
                    value = string.Empty;
                value = value.Trim();

                var customerAttributes = this.CustomerAttributes;
                CustomerAttribute firstNameAttr = customerAttributes.FindAttribute("DNI", this.CustomerId);
                if (firstNameAttr != null)
                {
                    firstNameAttr.Value = value;
                    IoC.Resolve<ICustomerService>().UpdateCustomerAttribute(firstNameAttr);
                }
                else
                {
                    firstNameAttr = new CustomerAttribute()
                    {
                        CustomerId = this.CustomerId,
                        Key = "DNI",
                        Value = value
                    };
                    IoC.Resolve<ICustomerService>().InsertCustomerAttribute(firstNameAttr);
                }
                ResetCachedValues();
            }
        }
    }
}

ok new file  


"As a good practice name the file something like MyAttributes.Customer.cs and place it in the same directory as Customer.cs "

But where do i call it ?
12 years ago
licass wrote:
But where do i call it ?


The new property now belongs to the Customer class, anywhere you have an instance of the Customer class you can call this property, that's how partial classes work, they allow you to have the code of one class in several files, so you can extend their behaviour.

In any of the ASP.NET code behind code files where the Customer class is used now you can do somethin like:

customer.DNI = txtDNI.Text;
11 years ago
How to do this in v2.4?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.