Developer forum

Forum » Ecommerce - Standard features » ProductListViewModel issue in 9.9.6

ProductListViewModel issue in 9.9.6

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

hi guys,

I have recently upgraded to 9.9.6 and a piece of code that used to work, is not working anymore.

I have a PromotionProducts paragraph that contains a Product type field.

I used to use this piece of code before the upgrade:

List<ProductViewModelelementProducts = Model.Item.GetValue("Products"as List<ProductViewModel>;

I have read the documentation here:  https://doc.dynamicweb.com/documentation-9/content/items/item-fields/product-catalog-item-field-type

and I have changed my code to this:

ProductListViewModel elementProducts = Model.Item.GetValue("ProductCatalog") as ProductListViewModel;

But no matter what I do, the elementProducts.Products is null

The old code is working in 9.9.4 but it is not working in 9.9.6.

What am I doing wrong?

Thank you,

Adrian 


Replies

 
Nicolai Pedersen
Reply

There is no changes between 9.9.4 and 9.9.6

There is 2 different field types - ProductCatalogEditor returns a ProductListViewModel

The List<ProductViewModel> comes from the ProductEditor.

BR Nicolai

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I cannot figure out which one is mine. I have attached a screenshot of the item definition.

I have probably created a big confusion by mixing versions in my original post.

My solution was upgraded from 9.4 to 9.9.6. The code was working in 9.4.

I have mentioned the code from 9.9.4 because I have tried to copy the code from another solution running 9.9.4 and use it in my solution (although the code from my original solution 9.4.7 and the code from 9.9.4 was identical).

I have to mention that all of the other properties of the ItemType are rendered correctly. This field is the only one with problems.

Thank you,

Adrian

ProductListViewModel.png
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I have investigated further and I don't believe the methods are wrong.

But I suspect something wrong with some cache when reading the values from that field or some processing of the products.

The products that were saved in the paragraph before the update, are no longer returned in the List<ProductViewModel>.

Why? I have no clue. I have tried with other products and they were displayed just fine, with the same code.

I have checked the products that are not displayed and they are Active, In stock and with price. Therefore no reason to be hidden.

I have also checked the assortments and they are included in the assortments. Products are visible in the store, except for this listing in the Promotion Products paragraph.

Any idea?

Adrian

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

One more thing.

The only major difference between products that are displayed and the ones that are not displayed, is that the not displayed ones have variants.

I am not sure if this makes any sense but I am running out of ideas and things to check.

Thank you,
Adrian

 

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I have encountered this problem again ina separate solution.

This time I was using the PromotionProducts Itemtype that was shipped with Rapido.

I have tried changing the Products field to ProductCatalog and Product but I've got the same result. Nothing was rendered in the output for the field, no matter what products I have selected. This time, the solution does not have variants.

In both cases, the solutions were upgraded to 9.9.6.

I have another few solutions that were installed directly on 9.9.6 and they don't have this issue.

In my previous case, I ended up avoiding the Model.Item.GetValue("Products") call and I had to instantiate the Item and look for the string value of the property in order to get the product. Not the cleanest solution but a temporary workaround until finding a permanent fix. In this solution, the lists would be used in landing pages and Homepage and I am afraid the workaround would affect the performance.

I am wondering if this has anything to do with a setting or data on the products.

Thank you,

Adrian

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Adrian

Well, can't tell what you are doing. See my setup for reference using this template:

@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel>

<div class="container">
    @{
        var productList = (Dynamicweb.Ecommerce.ProductCatalog.ProductListViewModel)Model.Item.GetValue("NewProductCatalogFieldProducts");
        foreach (var product in productList.Products)
        {
            <div>@product.Name - @product.Price.PriceFormatted</div>
            @*<pre>@product.ToJson()</pre>*@
        }
    }
    <pre>
     @productList.ToJson()
    </pre>
    <pre>
        @Model.ToJson()
    </pre>
</div>

When using viewmodels, you can always do a ToJson and get some information on what is available. Above I do it on the item model, the product list (which is also a viewmodel, but coming from one field of the item model), and on the product which is a viewmodel from the list of products on the product list viewmodel.

BR Nicolai

Screenshot_2021-02-01_102019.JPG Screenshot_2021-02-01_102035.JPG Screenshot_2021-02-01_102053.JPG
Votes for this answer: 1
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

This is extremely useful.

I have followed your code and settings (although they were identical) but in my case, the productList object is null.

I have uploaded a few screenshots.

The only thing left to try is to change the system name of the field from Products to something else.

Adrian

 

ProductModule-1.png ProductModule-2.png ProductModule-3.png
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I have tested again and your code worked. I had a typo when I have copied your code in my template.

This line made the difference:
var productList = (Dynamicweb.Ecommerce.ProductCatalog.ProductListViewModel)Model.Item.GetValue("Products");

I was using Dynamicweb.Ecommerce.Products in my template instead of Dynamicweb.Ecommerce.ProductCatalog

Thank you very much for clarifying it.

Adrian

 
Lars Hejgaard Sørensen Dynamicweb Employee
Lars Hejgaard Sørensen
Reply

This works fine for me in 9.10.13, but in 9.12.7 I get an error:

System.ArgumentNullException: Value cannot be null.
Parameter name: key

I am basically using the same code:

 

@if (field.SystemName.ToLower() == "Product_Selection".ToLower())
                {
                    {
                        var productList = (Dynamicweb.Ecommerce.ProductCatalog.ProductListViewModel)itemViewModel.GetValue("Product_Selection");

                    <tr>
                        <td colspan="2">@productList.ToString()</td>
                    </tr>
                    if (productList == null)
                    {
                        continue;
                    }
                    if (productList.Products == null)
                    {
                        continue;
                    }
                    <tr>
                        <td>&nbsp;-&nbsp;</td>
                        <td>
                            <table border="1">
                                @foreach (var product in productList.Products)
                                {
                                    <tr>
                                        <td>&nbsp;-&nbsp;</td>
                                        <td>@product.Name</td>
                                    </tr>
                                }
                            </table>
                        </td>
                    </tr>
}

Would I need another approach to accessing the products in the ProductListViewModel in 9.12+?

Also, using Model.ToJson fails in both versions, but in 9.10 I get this message:

Newtonsoft.Json.JsonSerializationException: Error getting value from 'Price' on 'Dynamicweb.Ecommerce.ProductCatalog.ProductViewModel'. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: key

Thinking there could be a relation between that and the error I get in 9.12.

Any suggestions are highly appreciated.

 

 

You must be logged in to post in the forum