-1

I'm trying to set & get the custom property values for a commerce user.
While fetching the value, there is an error:

The given key was not present in the dictionary

public void SetCommerceUserProfilePropertyValues(string usernameWithDomain, List<KeyValuePair<string, object>> keyValuePairs)
        {
            if (keyValuePairs != null && keyValuePairs.Count > 0)
            {
                Sitecore.Commerce.Entities.Customers.CommerceUser commerceUser = GetCommerceUser(usernameWithDomain);

                if (commerceUser != null)
                {
                    foreach (KeyValuePair<string, object> kvp in keyValuePairs)
                    {
                        commerceUser.SetPropertyValue(kvp.Key, kvp.Value);
                    }
                }
            }
        }

public object GetCommerceUserProfilePropertyValue(string usernameWithDomain, string propertyName)
        {
            CommerceUser commerceUser = GetCommerceUser(usernameWithDomain);
            return commerceUser != null ? commerceUser.GetPropertyValue(propertyName) : null;
        }

public void MyMethod()
{
    string usernameWithDomain = "Storefront\\myname@email.com";  
    List<KeyValuePair<string, object>> keyValuePairs = new List<KeyValuePair<string, object>> {
                        new KeyValuePair<string, object>("ForgotPasswordToken", Guid.NewGuid()),
                        new KeyValuePair<string, object>("ForgotPasswordTokenCreatedDate", DateTime.Now)
                    };
    
    ///save custom property values
    SetCommerceUserProfilePropertyValues(usernameWithDomain, keyValuePairs); 
    
    --Exception here
    var xtoken = GetCommerceUserProfilePropertyValue(usernameWithDomain, "ForgotPasswordToken") as string;
}

The commerceUser is not null and there is no error while setting the value.
Is there any "Save" action to be done just like it is done for Sitecore user profile - profile.Save()

Using XC 10.1

2
  • 1
    I think we can not set property in CommerceUser directly like this but you can set these property to Sitecore.Security.UserProfile, is there any specific reason you want to set these property to CommerceUser only? If yes, you can add a field to Customer Entity and retrieve from there. May 20, 2021 at 12:49
  • @GauravAgarwal After the for loop if I check commerceUser.GetProperties(), I'm able to see all the new properties.
    – sukesh
    May 20, 2021 at 12:58

1 Answer 1

2

We added phoneNumber in CommerceUser, for that we used below code. You can update as per your requirement. But yes you need to save it into Customer Entity using below code.

public bool UpdateAccountPhone(string phone, IContext sitecoreContext)
{
    bool result = false;

    // Update phone number
    CustomerServiceProvider customerProvider =
    (CustomerServiceProvider)Factory.CreateObject(nameof(customerProvider), true);

    var commerceUser = this.GetCommerceUser(sitecoreContext);
    if (commerceUser != null)
    {
       commerceUser.SetPropertyValue("Phone", phone);
       // Update the user
       var updateRequest = new UpdateUserRequest(commerceUser);
       customerProvider.UpdateUser(updateRequest);
       result = true;
   }

   return result;
}

Use namespace below

using Sitecore.Commerce.Services;
using Sitecore.Commerce.Services.Customers;

Then you can get property like below -

commerceUser.GetPropertyValue("Phone").ToString();
1
  • How do we do it for a non-logged in user.
    – sukesh
    May 20, 2021 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.