Copied to clipboard

Flag this post as spam?

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


  • Robin Hansen 135 posts 368 karma points
    Sep 08, 2016 @ 13:39
    Robin Hansen
    0

    Display Merchello Proucts with Umbraco nodes

    As head line says - Can I combine these two in one display...?

    Lests say I has these:

    Model.Content.GetPropertyValue<IEnumerable<IProductContent>>(propertyId)

    Model.Content.Descendants("article")

    How can I combine theese in one foreach loop...? (they both has the same properties)

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Sep 08, 2016 @ 15:41
    Rusty Swayne
    0

    Hi Robin,

    You have two distinctly different collections there - but they are both collections of IPublishedContent so you could merge the two together. There are lots of ways to do it - but to give you an idea:

      var combined = new List<IPublishedContent>();
    
      combined.AddRange(Model.Content.GetPropertyValue<IEnumerable<IProductContent>>(propertyId));
      combined.AddRange(Model.Content.Decendants("article"));
    
      foreach(var item in combined.OrderBy(x => x.Name))
      {
           // do something ...
      }
    

    Just FYI - if you're using Merchello 2.x you should change the

    Model.Content.GetPropertyValue<IEnumerable<IProductContent>>(propertyId)

    to

    Model.Content.GetPropertyValue<ProductContentListView>(propertyId) it's basically the same thing but has a bit more information and allows us to expose more targeted extension methods.

  • Robin Hansen 135 posts 368 karma points
    Sep 09, 2016 @ 13:53
    Robin Hansen
    0

    That kind of did the trick... - however, I used to get the Collections() property this way:

    foreach (var product in Model.Content.GetPropertyValue<IEnumerable<IProductContent>>("newestProducts"))
    {
        var collection = product.Collections().FirstOrDefault().ParentKey;
    }
    

    But with your example I get the message:

    'IPublishedContent' does not contain a definition for 'Collections' and the best extension method overload 'ProductCollectionExtensions.Collections(IProuctContent)' requires a resiver of type 'IProductContent'
    

    I really ned the value of Collections() :-|

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Sep 09, 2016 @ 15:35
    Rusty Swayne
    0

    Hey Robin,

    What Merchello version are you working with?

    In the FastTrack starter (I think) we do something similar to what you are trying to do... https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.FastTrack.Ui/Views/ftProduct.cshtml#L82

    However, notice that this is only being used on the Product page itself and not while iterating through a collection as, depending on how you have things setup, create a lot of SQL calls if your not careful.

    Also -

    In your code

     var collection = product.Collections().FirstOrDefault().ParentKey;
    

    You might consider checking for null before trying to read the parent

     Guid? parentKey;
     var collection = product.Collections().FirstOrDefault();
    
     parentKey = collection != null ? collection.Parent.Value : null;
    
  • Robin Hansen 135 posts 368 karma points
    Sep 10, 2016 @ 19:24
    Robin Hansen
    0

    I think I use Merchello ver. 2.1.0 - I'll upgrade to the latest version as soon as possible...

    As I explained - I cannot get collections this way while using the new setup:

    var collection = product.Collections().FirstOrDefault().ParentKey;

    However - what I used it for was to get the Products root collection name (grand parent) like this: (for use in css)

    var category = MerchelloContext.Current.Services.EntityCollectionService;
    var collection = product.Collections().FirstOrDefault().ParentKey;
    var categoryClass = category.GetByKey(new Guid(collection.ToString())).Name.ToLower();
    
    Root Collection      <--- this name
        -Sub Collection
                -Product <--- this product 
    

    Maybe your'e right - it might prodouce to many sql calls... - is there an easyer way to get the root collection name...?

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Sep 10, 2016 @ 20:22
    Rusty Swayne
    0

    Not in version 2.2.0 - In the 2.3.0 release we've added a bunch of functionality to the IProductCollection - https://github.com/Merchello/Merchello/pull/1793

    Still a ways out on releasing - but it's based largely on the discussion above - just has a bit more optimization. Let me know if you have problems in 2.2.0 ...

Please Sign in or register to post replies

Write your reply to:

Draft