Copied to clipboard

Flag this post as spam?

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


  • Tolu Jemilua 39 posts 164 karma points
    Aug 04, 2017 @ 08:31
    Tolu Jemilua
    0

    How to create collections under Products using code

    Is there any way to create a collection under Products Manually using code? Currently I am using IEntityCollectionsService as so.

    var collection = this.MerchelloServices.EntityCollectionService.CreateEntityCollectionWithKey(EntityType.Product, Guid.NewGuid(), "Iconic Collection");
                        this.MerchelloServices.EntityCollectionService.Save(collection);
    

    it saves into the EntityCollectionsTable in the database and doesn't appear under Products even though the entity type set is products, also when I refresh Merchello, its deleted from the table. Anyone have any idea on how to manually create a collection?

  • Puck Holshuijsen 82 posts 321 karma points
    Aug 04, 2017 @ 11:14
    Puck Holshuijsen
    101

    I believe it has to do with the Guid.NewGuid().

    If you change it to:

    Merchello.Core.Constants.ProviderKeys.EntityCollection.StaticProductCollectionProviderKey
    

    it should work I think.

    Puck

  • Tolu Jemilua 39 posts 164 karma points
    Aug 07, 2017 @ 12:21
    Tolu Jemilua
    0

    Thank you so much, I was battling with this all weekend, who would have thought this is all it took

  • Puck Holshuijsen 82 posts 321 karma points
    Aug 07, 2017 @ 12:35
    Puck Holshuijsen
    0

    Great to hear! Glad I was able to help

  • Tolu Jemilua 39 posts 164 karma points
    Aug 07, 2017 @ 14:26
    Tolu Jemilua
    0

    If you don't mind, I would also like to know how to create child collections, I tried using the collection keys create above as the provider key with no success

    currently I have:

    foreach (var subCollection in sec.Subsections) {
                    var sub = this.MerchelloServices.EntityCollectionService.CreateEntityCollectionWithKey(EntityType.Product, collection.Key, subCollection.Name);
                    this.MerchelloServices.EntityCollectionService.Save(sub);
                }
    

    I had hoped there would be a createChildCollection method against the collection

  • Puck Holshuijsen 82 posts 321 karma points
    Aug 07, 2017 @ 14:31
    Puck Holshuijsen
    3

    Basically what you have to do, is create a second collection, and pass the ParentKey to that collection.

    So in short:

    var parent = merchelloServices.EntityCollectionService.CreateEntityCollection(
                        EntityType.Product,
                        Core.Constants.ProviderKeys.EntityCollection.StaticProductCollectionProviderKey,
                        "parent collection");
    
            var child = merchelloServices.EntityCollectionService.CreateEntityCollection(
                        EntityType.Product,
                        Core.Constants.ProviderKeys.EntityCollection.StaticProductCollectionProviderKey,
                        "Child collection");
            child.ParentKey = parent.Key;
    
            // Save the collections
            merchelloServices.EntityCollectionService.Save(new[] { parent, child });
    

    That should do it

  • Simon 691 posts 1066 karma points
    Jul 02, 2019 @ 12:24
    Simon
    0

    Hello,

    I tried the exact code below, but when I go and find the new collection, I am getting this error :

    enter image description here

    Any help would much be appreciated what I am doing wrong.

Please Sign in or register to post replies

Write your reply to:

Draft