uCommerce set Category Definition Values and Description

663 views Asked by At

Using uCommerce v6.6, Umbraco v7

I'm having trouble setting the category's display name and a custom definition that I have created.

I'm receiving this error:

not-null property references a null or transient value UCommerce.EntitiesV2.Category.ProductCatalog

I think this is b/c there's a property in the CategoryDescription class,

public virtual int CategoryDescriptionId { get; protected set; }

But I don't know how to set this b/c usually when you create objects like this, once you save an ID is created for you (think EF).

Also I'm needing to set the custom definition for the category, "productNumber".

var parentCategory = catalog.Categories.First(x => x.Name.Equals(parentName));

var newCategory = new Category
{
      Name = product.Name,
      Definition = productDef,
      DisplayOnSite = true,
      ParentCategory = parentCategory,
      ProductCatalog = catalog
};

catalog.Categories.Add(newCategory);
catalog.Save();


var catDescription = new CategoryDescription()
{
      DisplayName = product.GetValue<string>("productName"),
      Category = newCategory,
};

catDescription.Save();  // ****errors out here*****

var catProperty = new CategoryProperty()
{
     Category = newCategory,
     DefinitionField = DefinitionField.FirstOrDefault(x => x.Name.Equals("productNumber")),
     Value = product.GetValue<string>("productNumber"),
};

catProperty.Save();

All my variables have data, meaning they're not null. It's on the save that's the null. newCategory is successfully created as well each and every time.

Class def for CategoryDescription

public class CategoryDescription : IEntity
{
    public CategoryDescription();

    public static bool operator !=(CategoryDescription x, CategoryDescription y);
    public static bool operator ==(CategoryDescription x, CategoryDescription y);

    public virtual Category Category { get; set; }
    public virtual int CategoryDescriptionId { get; protected set; }
    public virtual int? ContentId { get; set; }
    public virtual string CultureCode { get; set; }
    public virtual string Description { get; set; }
    public virtual string DisplayName { get; set; }
    public virtual int Id { get; }
    public virtual bool RenderAsContent { get; set; }

    public static IQueryable<CategoryDescription> All();
    public virtual void Delete();
    public static void Delete(Expression<Func<CategoryDescription, bool>> expression);
    public override bool Equals(object obj);
    public static bool Exists(Expression<Func<CategoryDescription, bool>> expression);
    public static IList<CategoryDescription> Find(Expression<Func<CategoryDescription, bool>> expression);
    public static CategoryDescription FirstOrDefault(Expression<Func<CategoryDescription, bool>> expression);
    public static CategoryDescription Get(object id);
    public override int GetHashCode();
    public virtual void Save();
    public static CategoryDescription SingleOrDefault(Expression<Func<CategoryDescription, bool>> expression);
}
1

There are 1 answers

4
Martin On

I recommend you use the method AddCategoryDescription on newCategory instance instead of trying to tie up the CategoryDescription with references manually. uCommerce is built on NHibernate and sometimes it can be difficult to find out which property is causing the trouble (As long you're not using stateless sessions; then you have to handle it).

I recall that uCommerce is set up to cascade all saves so if you call Save() at last on your catalog you should be good to go.

EDIT (To answer how to populate a property): You can populate a (new!) property value by using the following

var definitionField = DefinitionField.FirstOrDefault(x => !x.Deleted && x.Definition.Name == "MyDefinition");
var category = new Category();
category.AddProperty(new CategoryProperty
{
    Category = category,
    DefinitionField = definitionField,
    CultureCode = "en-GB",
    Value = "My value"
});

I haven't tested it but I sure it will work. If you want overwrite an existing property value you should find the CategoryProperty in the Category.CategoryProperties collection and then replace the Value property. Be aware if you create the same property twice since it can cause the backend YSOD (Unless they have fixed the unintended feature :) )

Best regards Martin