Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Dmitrij Jazel 86 posts 179 karma points
    Mar 29, 2013 @ 14:48
    Dmitrij Jazel
    0

    How to create category programmatically in C#? :)

    Hello Guys, question is quite simple, maybe someone has an answer to this one?

    Any kind of help is highly appreciated.

    What I eventually need, is that I want to be able to Populate the uCommerce shop with programmatically.

    I guess be able to create new Categories in my Default category definition group - fill it in.

    Than create products with default product definition, and than populate them into those categories...

     

    Isn't that a piece of cake? :-)

    I think it is for someone who did it before...

     

    Thanks alot for help!

    //Dmitrij

  • Martin 181 posts 740 karma points
    Mar 29, 2013 @ 16:20
    Martin
    0

    Hi Dimitrij,

    It's quite simplte. uCommerce is using a generic repository so you can actually use that. The most simple way to do it is resolve the repository you want (etc Category) via uCommerce's ObjectFactory and then fill in required information for category and save it through the repository. You just have to be aware that you have to set security settings for a user otherwise will it not show up at first.

    Example:

    var repository = ObjectFactory.Instance.Resolve>();

    var category = new Category() { "...set properties..." };

    repository.save(category);

     

    you can also try to take a look at this: http://blog.lasseeskildsen.net/post/uCommerce-How-To-Create-A-Product-Using-The-API.aspx (a bit more simple)

     

    Regards Martin

     

  • Dmitrij Jazel 86 posts 179 karma points
    Mar 29, 2013 @ 17:47
    Dmitrij Jazel
    0

    Hej Martin! :-)

    Thanks for info :-) well yes, I did checked the forum post, I saw it before, and yes, I can see it now (haven't seen before) there is example with category aswell.

    So way I understand it must be like so:

    1) create product

    2) create category

    3) create relation

    Creation of product - is pretty much streight forward, just one question, where do I get "ProductDefinitionId = 1,  // Set to an existing defintion" my compiler complains here obviously cause there is no ProductDefinitionId setup in the first place, where is he taking it from?

    Than Category, well pretty simple "var category = Category.SingleOrDefault(x => x.Name == "Software");" no questions here,

     

    Than the relation... well first of all why would you use {} instead of () if you are trying to create a new instance of the object, and not defining the method?

                var relation = new CategoryProductRelation
                                   {
                                       ProductId = product.ProductId,
                                       CategoryId = category.CategoryId
                                   };

    Those {} are confusing me. how am I supposed to implement the relation?

     

    //Dmitrij

  • Martin 181 posts 740 karma points
    Mar 29, 2013 @ 18:13
    Martin
    100

    Hi again,

    I recommend creating category first because a product has to live at least in one category (as I recall). ProductDefinitionId = 1 can be found several places. The easist way to find out is to navigate to your definition in uCommerce tree and hold your mouse over the definition name. Then in the bottom of your browser you should be able to see what javascript method is being called. It should contain the id.

    Another way is to find the id through SQL Server Management Studio and find the actually id.

    Last way is to fetch the category with uCommerce API based on the definition name and then get the id from the definition object.

    You are asking for using {} instead of (). I'm not quite sure whether you're asking for why I'm using object initializer (correct me if I'm misunderstanding). Try to look at this: http://weblogs.asp.net/dwahlin/archive/2007/09/09/c-3-0-features-object-initializers.aspx it will explain usage of object initializer.'

    Best Regards Martin

  • Dmitrij Jazel 86 posts 179 karma points
    Mar 29, 2013 @ 18:17
    Dmitrij Jazel
    0

    Hej Martin,

    Nah... it's all ok, now I see, that It is actual .net functionality, not sure why i haven't used it before in such a way :-/ and thanks for one more link and info... now it makes much more sense :)))

    I will try doing what I can with all this and will let you know if I have any trouble.

     

    Thanks so far,

    Dmitrij :-)

  • Dmitrij Jazel 86 posts 179 karma points
    Mar 29, 2013 @ 18:36
    Dmitrij Jazel
    0

    Hmm, but still is not beying assigned :-? for some reason, UCommerce.EntitiesV2 does not contain any ProductDefinitionId  , it contains ProductDefinition but not the other one. Any suggestions? :-(

    If I am trying to set ProductDefinition to = 1 - than compiler obviously compains cause you cannot set ProductDefinition to int... 

     

    I am using UCommerce.EntitiesV2, not sure what else should I do :( stuck again :(

     

    //Dmitrij

  • Martin 181 posts 740 karma points
    Mar 29, 2013 @ 19:31
    Martin
    0

    ProductDefinition is the one you need to get. Then you can fetch the id from ProductDefinition. I think it has a property called ProductDefinitionId but I'm not sure on hand.

  • Dmitrij Jazel 86 posts 179 karma points
    Mar 29, 2013 @ 21:30
    Dmitrij Jazel
    0

    Hello again Martin,

    Sorry for my question, and thanks for your patience.

    But you are saying here that: 

    "ProductDefinition is the one you need to get. Then you can fetch the id from ProductDefinition."

    ok and this is what I see: 

    If that is what I need - it has no reference nor to ID nor to ProductDefinitionId ... 

     

    Instead Lassees article suggests to use 

    But as you can see I am getting an error :-/

     

    //Dmitrij

     

  • Martin 181 posts 740 karma points
    Mar 29, 2013 @ 23:36
    Martin
    0

    Hi Dmitrij,

    ProductDefinition property on Product-class is a reference to a productdefinition. One way to set the definition :

    var productDefinition = ProductDefinition.SingleOrDefault(x => x.Name == "Definition name");

     

    Then you can say

    var product = new Product

    {

    ProductDefinition = productDefinition,

    }

     

    This way NHibernate will make sure to set the correct references in database tables.

  • Dmitrij Jazel 86 posts 179 karma points
    Apr 01, 2013 @ 16:06
    Dmitrij Jazel
    0

    Hej Martin,

    Nice!!! I think I got it now :) It works perfectly now, thanks alot for help! :-)

     

    Have a great day mate,

    Dmitrij

  • Dmitrij Jazel 86 posts 179 karma points
    Apr 01, 2013 @ 17:41
    Dmitrij Jazel
    0

    Just to share my results on this one with the community

    This is the code I am using and it works for me so far :-)

     

    1) In my example, I am creating new product in existing category

    2) I create new caterogy and add it into the catalog.

    //1) Create new product
    
                // Get existing product definition - product requires one
                var productDefinition = ProductDefinition.SingleOrDefault(x => x.Name == "BasicProduct");
                // Get exsisting category called "Cars"
                var category = Category.SingleOrDefault(x => x.Name == "Cars");
    
                // Create new product
                var product = new Product
                {
                    Sku = "SKU-123456",
                    Name = "My Product",
                    AllowOrdering = true,
                    DisplayOnSite = true,
                    ProductDefinition = productDefinition  // Set to an existing defintion
                };
                product.Save();
    
                // Add product relation to category
                var relation = new CategoryProductRelation
                {
                    Product = product,
                    Category = category
                };
                relation.Save();
    
    
                //2) Create totally new Category
    
                // get existing Catalog that will contain the our new category
                var catalog = ProductCatalog.SingleOrDefault(x => x.Name == "ShopCatalog");
                // get existing category definition by Name
                var definition = Definition.SingleOrDefault(x => x.Name == "Default Category Definition");
    
                // create new category
                var category2 = new Category
                {
                    Name = "Bikes",
                    Definition = definition
                };
    
                // add Category to the catalog and save.
                catalog.AddCategory(category2);
                catalog.Save();
Please Sign in or register to post replies

Write your reply to:

Draft