Developer forum

Forum » CMS - Standard features » ProductPicker in Item ViewModel

ProductPicker in Item ViewModel

Claus Kølbæk
Claus Kølbæk
Reply

Hey

I am trying to create a product picker item (sorta like the one you use in your newsletter on rapido). - and since it is not mentioned on the dokumentation page - I tried to follow the code mentioned by Morten here: https://doc.dynamicweb.com/forum/ecommerce-standard-features/ecommerce-standard-features/new-item-type

so if I build the following:

var products = Model.Item.GetValue("Products"as IList<Dynamicweb.Ecommerce.Frontend.ProductViewModel>;
	if (products.Any())
	{
		<div>
			@foreach (var product in products)
			{
				<div>@product.ToJson()</div>
			}
		</div>
	}

I end up with the following:

{"Id":"PROD2","VariantId":"","LanguageId":"LANG1","Name":"testr","Number":"1231231231"}

Which is not a great deal of product values :) - if I try calling product.price.PriceFormattet or something similar it seems to just fail.

I can also see that Dynamicweb.Ecommerce.Frontend.ProductViewModel is deprecated, but changing it to Dynamicweb.Ecommerce.ProductCatalog.ProductViewModel results in 0 values as the type from the item is the deprecated one.

So any way of getting this to work? - The post I found is a bit older, so maybe there is a better way of doing this now.?


Replies

 
Martin Vang
Martin Vang
Reply

Hi Claus,

The old models where deprecated because they only contained that information - and because they where incompatible with the framework that delivers product data for viewmodels. So, you will never be able to use those to achieve your desired result.

What you can do instead, is to use a cshtml with the correct viewmodel as a Model or to fetch the ProductListViewModel from the webapi (use "/dwapi/Products/GetProductList?ProductIds=[PROD65,PROD70]&LanguageId=[LANG1]&CurrencyCode=[EUR]&CountryCode=[DK]&FilledProperties=&ShopId=[SHOPX]", where you replace the things in [] with what you need)

The documentation side you reference should contain a guide to using the new viewmodel in your template.

 

Let me know how it goes.

BR

Martin

 
Claus Kølbæk
Claus Kølbæk
Reply

Hi Martin

I'll try with the webapi I think, granted as the Product selector allows you to select from more than one shop, it might have some minor issues, but should be fine for this project.

Your other option "using a cshtml with the correct viewmodel as Model" how would you go about doing that? - in my example above the product is the correct viewmodel is it not? It is atleast the one supplied by the item.

 
Martin Vang
Martin Vang
Reply

Hi Claus,

Currently the only app designed to work with the new models is the Product catalog for Viewmodels. You can find some help with how that works in the docsite: https://doc.dynamicweb.com/documentation-9/ecommerce/apps/product-catalog-for-viewmodels.

You wont be able to use the Model in your template to work with ProductViewModel objects in our current codebase. If this is desired, it will need to be requested. Sorry.

 

BR

Martin

 
Claus Kølbæk
Claus Kølbæk
Reply

Alright, yea I have used the product-catalog so no issues there - my problem atm. is only related to the ItemType Product.

It gives me the product ID though, so I guess Ill just have to look up the rest of the data with an extra call for now, maybe through the webapi as you also mentioned. - It would make sense to have something like Model.Item.GetProduct or something similar though. So consider that a request if you will :)

 
Martin Vang
Martin Vang
Reply
This post has been marked as an answer

Hi Claus,

I'll keep a mental note of this - if it needs to be a formal request, we need to get it in the requests part of the forum.

Something you can consider while you wait, is if you can use something like this:

public static class ProductViewModelExtender
    {
        public static IList<Product> GetProducts(this IList<ProductViewModel> viewModels)
        {
            var result = new List<Product>();
            var languageId = Ecommerce.Common.Context.LanguageID;
            var productIds = new List<string>();            
            foreach(var view in viewModels)
            {
                productIds.Add(view.Id);
            }
            var products = Ecommerce.Services.Products.GetByProductIDs(productIds.ToArray(), false, languageId, false, false);
            result.AddRange(products);
            return result;
        }
    }

If you include this in the using on the template, you can do a .GetProducts like you want, and get the full products from the api in the language that you are viewing the page on.

Let me know what you think.

BR

Martin

Votes for this answer: 1
 
Claus Kølbæk
Claus Kølbæk
Reply

Hey Martin

I ended up making something very similar to what you just wrote here. - I can make another post in the request forum if it helps aswell. My point mainly is that without either the webapi or a solution like u posted now - the current use of the Product Type on the Item is useless smiley - which is I think is the only type that is the case for atm. Actually it is even pretty similar in how it behaves  (Well from a none technical point at least) as the type for Users - which you have a Model.Item.GetUsers for which currently returns a list of UserViewModels.
Anyways it works for me with this - so I have marked your post as the answer :)

 
Martin Vang
Martin Vang
Reply

Hi Claus,

Thanks for the explaination - I'll link this thread and create a feature request, but no guarantees about when/if it's going to be prioritized.

Feature 73855: Make ProductPicker itemtype more useable in frontend

Best of luck with your project.

BR

Martin

 

You must be logged in to post in the forum