Developer forum

Forum » Ecommerce - Standard features » Get product info from paragraph template

Get product info from paragraph template

Thomas Clausen
Reply

Hi

I'm in the process of upgrading a Dynamicweb solution from v 8.X to v 9.4.
In the new version theres been some changes to the API, as listed here: https://doc.dynamicweb.com/downloads/releases/upgrading/dw9-api-cheatsheet

Amongst those is the change fra "eCommerce" to "Ecommerce", but also the Product.GetProductById method (https://doc.dynamicweb.com/api/html/bc85a251-d638-d667-f30b-5a9a0c96723a.htm) is now obsolete.
Unfortunately there is no reference to which method that can be used instead.

I'm trying to get some product info (ex. name, productnumber and categories) from within a paragraph template using the following code:

@{
    string productid = System.Web.HttpContext.Current.Request.QueryString.Get("ProductID");
    Dynamicweb.Ecommerce.Products.Product p = Dynamicweb.Ecommerce.Products.Product.GetProductByID(productid);

    string productname = p.Name;
    string productnumber = p.Number;
    string categoryName = p.GetCategories()[0].Name.ToLower();
}

The page this paragraph is inserted on is a page that displays a product.

I found the method ProductService.GetProductById (https://doc.dynamicweb.com/api/html/f273417e-3def-f13c-f4f4-77bf8c8215d6.htm), but this method takes three parameters instead of just the product id, and requires a product variant id - what if the product isn't a variant?

I have tried to modify my code to this instead:

@{
    string productid = System.Web.HttpContext.Current.Request.QueryString.Get("ProductID");
    Dynamicweb.Ecommerce.Products.Product p = Dynamicweb.Ecommerce.Products.ProductService.GetProductById(productid, "", true);

    string productname = p.Name;
    string productnumber = p.Number;
    string categoryName = p.GetCategories()[0].Name.ToLower();
}

But it returns this error message:

An object reference is required for the non-static field, method, or property 'Dynamicweb.Ecommerce.Products.ProductService.GetProductById(string, string, bool)'

 

Any suggestions as to how to get some product info from a paragraph template or what I'm doing wrong?

Best regards
Thomas


Replies

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

You almost had it. Try this...

@{
    string productid = System.Web.HttpContext.Current.Request.QueryString.Get("ProductID");
    Dynamicweb.Ecommerce.Products.Product p = Dynamicweb.Ecommerce.Services.Products.GetProductById(productid, "", true);

    string productname = p.Name;
    string productnumber = p.Number;
    string categoryName = p.GetCategories()[0].Name.ToLower();
}

But I am wondering why you need to do this in the first place.
What are you trying to achieve and why don't you use the product catalog templates for this?

Best regards,
Morten

Votes for this answer: 1
 
Thomas Clausen
Reply

It works - thank you.

We use some of the product info - like product number and category - on paragraphs with other modules added.
Ex. paragraphs with the form or the map module.

 

You must be logged in to post in the forum