Copied to clipboard

Flag this post as spam?

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


  • Simon 691 posts 1066 karma points
    Aug 18, 2016 @ 08:58
    Simon
    0

    Get Product Collections - Product Display

    Hi guys,

    Can anyone guide me on how to get the collections names that the particular products is in, from a ProductDisplay object? Or is there another way please?

    And Even in the search, how can I search for products in particular collection please?

     var searchedProducts = merchelloHelper.Query.Product.Search(1, modelAsProductsModel.ProductsPerPage).Items.Select(x => (ProductDisplay)x);
    

    thank you in advance

    Kind Regards

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Aug 18, 2016 @ 09:38
    Simon Dingley
    0

    Hi Simon,

    It doesn't look like there is an easy way to do it at present. Looking in the source there is some internal methods that do it but you can't use those as they are not public and potentially subject to change.

    Alternatively I guess you might have to do it the other way around and get all of the collections from the EntityCollectionService and find instances of your product in the collections but I'm not sure how well that will perform.

    Rusty might be able to offer a better insight into this when he is around.

    Cheers, Simon

  • Simon 691 posts 1066 karma points
    Aug 18, 2016 @ 09:45
    Simon
    0

    Thanks Simon,

    Even there is no way how I can get list of collections that a product falls under?

    for example, I want to list products, and underneath each product, need to show the categories.

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Aug 18, 2016 @ 10:39
    Simon Dingley
    100

    Wow, that seemed harder than it should have been but I got there in the end! This will get you a list of names of the collections a product is in...

    var entityCollectionSvc = MerchelloServices.EntityCollectionService;
    
    var merchelloHelper = new MerchelloHelper();
    var productKey = product.Key;
    
    // Get all product collections
    var productCollections =
        entityCollectionSvc.GetByEntityTfKey(Merchello.Core.Constants.TypeFieldKeys.Entity.ProductKey)
            .Where(x => merchelloHelper.TypedProductContentFromCollection(x.Key).Any(p => p.Key == productKey))
            .Select(c => c.Name)
            .ToArray();
    

    I would be interested to see any alternative method of achieving this as well.

    To answer the other part of your question you can perhaps use:

    merchelloHelper.TypedProductContentFromCollection(collectionKey).Where(x => x.Name == "some product name")
    
  • Simon 691 posts 1066 karma points
    Sep 30, 2016 @ 16:13
    Simon
    0

    Hi Simon,

    May I ask you whether I can get an IEnumerable of products where its collections are in another list that I will have List of categories ids?

    So that I can get a list of products which are in particular collections?

    Do you have an idea to share with mate?

    Thank you

  • Simon 691 posts 1066 karma points
    Dec 14, 2016 @ 15:13
    Simon
    0

    An Alternative, as Merchello 2.3.2:

     var collections =
                    merchelloHelper.Collections.Product.GetCollectionsContainingProduct(productKey)
                        .Where(x => x.ParentKey == collectionGuidToFilterBy).Select(x => x.Name.ToString());
    
                var filterdProductCollectionsToBeRemoved = new List<string>();
    
                var enumerable = collections as string[] ?? collections.ToArray();
                foreach (var p in enumerable)
                {
                    filterdProductCollectionsToBeRemoved.AddRange(
                        from cat in
                            _umbracoHelperService.TypedContent(umbracoHelper, ConstantValues.ProductsPageId)
                                .Children()
                                .Where(x => x.DocumentTypeAlias == ConstantValues.Document_Type_Alias_Product_Category)
                        where cat.Name.Equals(p)
                        select p);
                }
    
  • Revti Shah 7 posts 75 karma points
    Jun 04, 2019 @ 12:07
    Revti Shah
    0

    Hello Simon,

    This code which you gave, where should I put in my code like searchHelper.cs or SearchController.cs ? Have you used examine search ?

    Here is the link: https://codeshare.co.uk/blog/how-to-search-by-document-type-and-property-in-umbraco/

    But Still get only link output. I want Product search like ecommerce websites have...

    Thanks Revti

  • Simon 691 posts 1066 karma points
    Aug 18, 2016 @ 11:04
    Simon
    0

    Cheers Simon :)

    Appreciate

  • Simon 691 posts 1066 karma points
    Aug 22, 2016 @ 12:29
    Simon
    0

    Hi Simon,

    How can I get products from multiple collections, through the Umbraco Node Id where you have the collection picker?

    So basically, my search will return number of ids, (category ids) and those nodes has a list of products picker of merchello.

    Now I want to get products (distinctly) from those collections.

    Is it possible in some way?

    Thank you

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Aug 24, 2016 @ 13:53
    Simon Dingley
    0

    You can get the collection key from the collection picker property on your node and then load up the products for the collection using code in the above examples. Once you have done that you can return a distinct list.

    Whilst my solution should work it doesn't feel very efficient but given that v2.2.0 was just released I don't know if there is a better or easier way of now achieving this.

  • Simon 691 posts 1066 karma points
    Aug 22, 2016 @ 13:04
    Simon
    0

    Hi,

    Regarding the above, can I search for products in particular collections through the Search Function, in the where clause ,in order to keep pagination etc...

    like this

            var returnedProducts = merchelloHelper.Query.Product.Search(1, modelAsProductsModel.ProductsPerPage).Items.Select(x => (ProductDisplay)x).Where(.............);
    
  • Simon Dingley 1388 posts 3239 karma points c-trib
    Aug 24, 2016 @ 13:48
    Simon Dingley
    0

    can I search for products in particular collections through the Search Function, in the where clause ,in order to keep pagination etc...

    Rusty would need to confirm but at the moment no I don't believe you can do this via the existing search.

  • Simon 691 posts 1066 karma points
    Sep 22, 2016 @ 09:45
    Simon
    0

    Hi Simon,

    When doing a search in Merchello and specify which page you are requesting (getting the current page from the ?page query string), are there a way how I can get the total nodes, or all pages?

    Because I want to know the total number nodes of all pages of a current search in order to display a pager.

    Thanks

    Kind Regards

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Sep 22, 2016 @ 11:52
    Simon Dingley
    0

    I'm not paging the products on this site as the store is not all that big.

  • Simon 691 posts 1066 karma points
    Sep 22, 2016 @ 11:54
    Simon
    0

    Hi Simon

    Thank you for your reply.

    I would like to know to achieve it but? Do you have any clue?

    My question is how to get all the results / total results when searching and specifying paging? For example when I say page 1, I would like to also know what is the actual total result.

    Thank you Simon in advance for your help,

    Kind Regards

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Sep 22, 2016 @ 12:13
    Simon Dingley
    0

    That is all available via the returned QueryResultDisplay object in this part:

    merchelloHelper.Query.Product.Search(1, modelAsProductsModel.ProductsPerPage)
    

    You have properties for the following:

    • CurrentPage
    • ItemsPerPage
    • TotalItems
    • TotalPages

    Simon

  • Simon 691 posts 1066 karma points
    Sep 22, 2016 @ 12:14
    Simon
    0

    Thanks Simon :)

    Appreciate.

  • Simon 691 posts 1066 karma points
    Sep 23, 2016 @ 09:58
    Simon
    0

    Hi Simon,

    Buf if I want to get the Items and check whether the name matches a particular string... how can I still get the totalItems?

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Sep 23, 2016 @ 10:01
    Simon Dingley
    0

    You can still access the items from that QueryResultDisplay so query the Items collection to find one matching your search string as you would have done before? Unless I'm missing the point?

  • Simon 691 posts 1066 karma points
    Sep 23, 2016 @ 10:04
    Simon
    0

    Hi Simon, But if I do something like the below:

    var queryResultDisplaytest = merchelloHelper.Query.Product.Search(currentPageAsInteger, modelAsProductsModel.ProductsPerPage).Items.Select(x => (ProductDisplay)x).Where(x => x.Name.ToLower().Contains("juice"));
    

    how can I still get the total pages and total items etc...?

    Thank you

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Sep 23, 2016 @ 10:14
    Simon Dingley
    0
    var queryResultDisplaytest = merchelloHelper.Query.Product.Search(currentPageAsInteger, modelAsProductsModel.ProductsPerPage)
    var queryProducts = queryResultDisplaytest.Items.Select(x => (ProductDisplay)x).Where(x => x.Name.ToLower().Contains("juice"));
    
    @queryResultDisplaytest.TotalItems
    @queryResultDisplaytest.TotalPages
    

    The above allows you to get totals for the original search results whilst still maintaining access to the original results.

    What I think you are assuming you can have is a paged list of items after you filtered the search results? In which case the TotalItems and TotalPages items are meaningless because you have filtered them again after the search results have been returned. You need to do your filtering in the original search query.

  • Simon 691 posts 1066 karma points
    Sep 23, 2016 @ 10:37
    Simon
    0

    Hi Simon,

    thank you for your reply.

    What I want is quite common.

    I Want to get the total items and pages etc... after I define my search and not after a general Search, which is basically getting all the products.

    My questions is after search by product name, can I have the new total pages of the result and the new total pages of the result etc..?

    Thank you in advance.

  • Simon Dingley 1388 posts 3239 karma points c-trib
    Sep 23, 2016 @ 11:05
    Simon Dingley
    0

    In that case I can't help you right now and would have to defer to Rusty or someone else on the core team, sorry.

  • Biagio Paruolo 1456 posts 1595 karma points c-trib
    Sep 23, 2016 @ 16:30
Please Sign in or register to post replies

Write your reply to:

Draft