Developer forum

Forum » Ecommerce - Standard features » Query an array

Query an array

Bjørn Kamfjord
Reply

Hi. I have a search function querying my product reposistory for productId, name etc.

Is there a way to extend the functionality, so that I could query an array of productids? Instad of only search for one product at a time, i would like to be able to put in multiple product ids and the have a list with the result.

Search: 1000,1001,1002,1003,1004

Result: Product 1, Product 2, Product 3, Product 4

I know this is not really a search, since the user already knows the IDs but the salespersons on the B2B shop in question uses this to create list for customers.

How can I achieve something like this?


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi Bjørn,

 

You should be able to do that by:

 

Best Regards,

Nuno Aguiar

Votes for this answer: 1
 
Bjørn Kamfjord
Reply

Changing my expression to IN did the trick! Super thanks!

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

You're welcome

 
Bjørn Kamfjord
Reply

Follow up, do you if its is possible to add multiple products  at the same time to PDF Catalog using CatalogPublishingcmd=addtocatalog ?

So once I have my list result, I can just click a button that says, "add all to catalog" or something?

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Bjørn,

 

I haven't tried it. Maybe somebody else has. I know you can add multiple items to your cart though. You can try to use the same logic.

 

You can check the documentation here https://doc.dynamicweb.com/documentation-9/ecommerce/orders/shopping-cart#2948

 

Nuno Aguiar

 
Bjørn Kamfjord
Reply

Hey. once again a follow up...

When searching for a string of product numbers, like the one below:

2025081,2025080,2024402,2024404,2024691,2024680,2024661,2024320,2024290,2024280

I want the result to be in the exact same order as the search? I was looking to the sort options in the catalog app, (I use an index for the products). But there is not an option for "search".
Any clue as to how I can achieve this?

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Bjørn,

 

That's some custom sorting that I don't think you can achieve with the index. If you are able to get the results in a single page, you can use Razor or Javascript to do it though.

 

Best Regards,

Nuno

 
Nicolai Pedersen
Reply

Lucene does not support that.

If you never provide an array larger than the collection of products you get out (just one page) you can sort the resulting collection after it has been queried.

BR Nicolai

 
Bjørn Kamfjord
Reply

My fears confirmed... 😭

How would you sort it with Razor ?

foreach(var result in GetLoop("Products")) ?? using Linq or something like that?

 

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

You can't either do it with LINQ and/or assign the GetLoop to a var and then sort the List<T> using various techniques. It's standard C# from then on.

 
Bjørn Kamfjord
Reply

I just don't really see what parameter or whatnot to use to sort the list?

 
Nicolai Pedersen
Reply

Hi Bjørn

I do not know of an easy way to sort a list based on an array of other values - so you either have to make your own IComparer for the list or dictionary.

Or do something like this - pseudo code :-):

var productIds = {2025081,2025080,2024402,2024404,2024691,2024680,2024661,2024320,2024290,2024280}
var sortedProducts = new List<product>();
var unSortedProducts = GetLoop("Products").ToDictionary(product => product.ProductID);

for each productid in productIds {
   sortedProducts.add(productid, unSortedProducts.Item[productid]);
}

for each product in sortedProducts.Values(){
   //write it out....
}

 

You must be logged in to post in the forum