Developer forum

Forum » Ecommerce - Standard features » New item type
Ivan Marijanović
Ivan Marijanović
Reply

Hi there,

I created new Item type and would like to combine the products groups and indvidual links to blogs. On this new item type I added several paragraphs which I use to enter info and make it link to the blog and also several items of type "Product" which I would like to use to add product groups and render it as links to products groups so that user can have in one paragraph links to products and/or blogs as he wish.

Issue I am having is that even I add some product groups to Product item type when I try to access individual products-groups like this:

@foreach(var groups in @Model.Item.GetItems("Products2"))

 I allways get error: 

Exception in template (\Designs\Rapido\Paragraph\CombinedProdAndBlogs.cshtml): System.NullReferenceException: Object reference not set to an instance of an object."

I can access other elements on page like this: Model.Item.GetItem("Blogs_1").GetString("Link") but cannot get around to access linked products groups.

Thank you in advance!

Ivan


Replies

 
Nicolai Pedersen
Reply

Hi Ivan

You need to show us the entire item type definition (screendump and maybe also add the XML definition in zip). And also the entire template.

Thanks, Nicolai

 
Ivan Marijanović
Ivan Marijanović
Reply

Hi Nicolai

Thank you for quick response.

Attached I am sending screenshot of Item definition (where I can find xml) and also template I am using to render view.

Ivan

ItemType_Definition.png
 
Nicolai Pedersen
Reply

Hi Ivan

The item type xml can be found in filemanager, /System/ItemTypes

Please attach your template in zip as well. cshtml files are not accepted as a download for security reasons...

Thanks, Nicolai

 
Ivan Marijanović
Ivan Marijanović
Reply

Here it is!

Ivan

 
Nicolai Pedersen
Reply

You use "GetItem" method on a field of type product - that will not work, since that will give you an itemViewModel.

Please read the documentation on how to use viewmodels found here: https://doc.dynamicweb.com/template-tags/introduction/concept/viewmodels

When you have a product you need to use the generic GetValue: https://doc.dynamicweb.com/api/html/9ccccd1a-f1f5-567e-1654-7d7a974c8d4b.htm

In 9.4 like this: @Model.Item.GetValue<Dynamicweb.Ecommerce.Frontend.ProductViewModel>("Products2")
In 9.5 like this: @Model.Item.GetValue<Dynamicweb.Ecommerce.ProductCatalog.ProductViewModel>("Products2")

BR Nicolai

 
Ivan Marijanović
Ivan Marijanović
Reply

Hi

Thanks for reference.

When I try that I cannot access individual products because I get error saying there is no GetEnumerator available. Shouldn't this contain the list of products that was referenced in item.

I am going with this:

var products = Model.Item.GetValue<Dynamicweb.Ecommerce.Frontend.ProductViewModel>("Products2");

foreach(var product in products)
{

}

Ivan

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

The value of a "Product" item field is a collection of products (IList<ProductViewModel>).

You can get the value like this...

var products = Model.Item.GetValue<IList<Dynamicweb.Ecommerce.Frontend.ProductViewModel>>("Products");

Or like this...

var products = Model.Item.GetValue("Products"as IList<Dynamicweb.Ecommerce.Frontend.ProductViewModel>;

Here is a more complete example:

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

 

Best regards,
Morten

Votes for this answer: 1
 
Ivan Marijanović
Ivan Marijanović
Reply

Thank you for your response.

I still cannot get what I want since I get this error when I try to use product.ShortDescription:

Line 193: 'Dynamicweb.Ecommerce.Frontend.ProductViewModel' does not contain a definition for 'ShortDescription' and no extension method 'ShortDescription' accepting a first argument of type 'Dynamicweb.Ecommerce.Frontend.ProductViewModel' could be found (are you missing a using directive or an assembly reference?)

When I look at documentation (https://doc.dynamicweb.com/api/html/cebf3de7-0a41-d50e-b4c7-04c78b73f8d6.htm ) this field is defined in ProductViewModel.

Also if I select groups instead of products which view could I use to get access to Group Name, Description, Link and Image like when using Item Publisher app.

Ivan

 
Ivan Marijanović
Ivan Marijanović
Reply

Hi Morten

any follow up on this. It seems I can access only Name, and id number but cannot create link to this product page!

Ivan

 
Nicolai Pedersen
Reply

Hi Ivan

You might have an earlier version of Dynamicweb that does not have all the properties. So maybe you need to update?

BR Nicolai

 

You must be logged in to post in the forum