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);
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.
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:
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);
}
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.
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(.............);
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.
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.
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?
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.
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?
thank you in advance
Kind Regards
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
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.
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...
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:
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
An Alternative, as Merchello 2.3.2:
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
Cheers Simon :)
Appreciate
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
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.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
Rusty would need to confirm but at the moment no I don't believe you can do this via the existing search.
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
I'm not paging the products on this site as the store is not all that big.
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
That is all available via the returned
QueryResultDisplay
object in this part:You have properties for the following:
Simon
Thanks Simon :)
Appreciate.
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?
You can still access the items from that
QueryResultDisplay
so query theItems
collection to find one matching your search string as you would have done before? Unless I'm missing the point?Hi Simon, But if I do something like the below:
how can I still get the total pages and total items etc...?
Thank you
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.
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.
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.
See this solution: https://our.umbraco.org/projects/collaboration/merchello/merchello/79391-solution-if-you-need-to-access-to-a-merchello-collection-from-a-page
is working on a reply...