Developer forum

Forum » Templates » ProductService gives error

ProductService gives error

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

I am trying to get product information on the default variant of a product.

 My template looks like this

@{
  string langId=GetString("Ecom:Product.LanguageID");
  string productidForVariantSearch=@GetString("Ecom:Product.ID");
  string defaultVariantId=@GetString("Ecom:Product.DefaultVariantComboID");
  var defaultProduct=ProductService.GetProductById("@defaultVariantId", "@productidForVariantSearch", "@langId");
 }

lanIs is = "LANG1" 
productidForVariantSearch is = "11210-10043" 
defaultVariantId is ="VO106.VO2"

 

However, I get ann error when trying to declare defaultProduct. The error says:

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

 

What am I doing wrong?

/Hans

 


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

You're treating ProductService as a class with a static method GetProductById which isn't correct. You should instead use an existing instance:

Dynamicweb.Ecommerce.Services.Products.GetProductById(...)

Also, you need to use the variables you created as  .NET variables. Currently you use them as string literals. So the final solution would look like this:

var defaultProduct = Dynamicweb.Ecommerce.Services.Products.GetProductById(.defaultVariantId, productidForVariantSearch, langId);

Hope this helps,

Imar

 

 

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Perfect Imar, thank you.

If I want to get a value from the defaultProduct, how can i get this?

I tried

@{
string defaultimage=defaultProduct["PrimaryImage"].ToString();
}

but that gives the error "Cannot apply indexing with [] to an expression of type 'Product'"

/Hans
 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

If Product has a PrimaryImage property, all you need is this:

string defaultimage = defaultProduct.PrimaryImage;

Imar

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you Imar

it looks like my defaultProduct object is empty.

 

I am trying @defaultProduct.Active - and that should have some value according to the documentation https://doc.dynamicweb.com/api/html/47e46310-6920-7861-849f-e8ac081269b0.htm, but I get an error stating - Object reference not set to an instance of an object.

what I am doing wrong?

/Hans

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Yep, looks like the product isn't found. Are you sure your values are correct? Can you print them out on screen before using them in GetProductById? That method ultimately executes something like this:

      productSql.Add("SELECT * FROM EcomProducts WITH (NOLOCK) WHERE ProductID = {0} ", (object) productId);
      if (string.IsNullOrEmpty(productVariantId))
        productSql.Add("AND (ProductVariantID IS NULL OR ProductVariantID = '') ");
      else
        productSql.Add("AND ProductVariantID = {0} ", (object) productVariantId);
      productSql.Add("AND ProductLanguageID = {0} ", (object) productLanguageId);
      return productSql;

so it looks for the ID, Variant ID and Language ID in the EcomProducts table. Confirming your values may tell you what's going on.

Imar

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

And if it still doesn't work, can you supply your latest code?

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you for your time Imar

ID, Variant ID and Language ID give these results:

Language ID=LANG1 
Product ID = 11210-10043
Variant ID = VO106.VO2


@{
string langId=GetString("Ecom:Product.LanguageID");
string productidForVariantSearch=@GetString("Ecom:Product.ID");
string defaultVariantId=@GetString("Ecom:Product.DefaultVariantComboID");
var defaultProduct = Dynamicweb.Ecommerce.Services.Products.GetProductById(defaultVariantId, productidForVariantSearch, langId);
//string idtest = defaultProduct.Id;
}

 

The outcommentet string ("idtest") gives an error statitng the object does not exist:

 

/Hans

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

Two things you want to change:

1. Get rid of the @ symbol in front of the two calls to GetString (as also discussed in the other forum post)

2. (This will make you go "duh!"): change the order of the parameters in this code:  Dynamicweb.Ecommerce.Services.Products.GetProductById(defaultVariantId, productidForVariantSearch, langId);

The correct list of parameters to GetProductById is: productId, variantId, languageId and you are specifying them as variantId, productId, languageId instead now.

Imar

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

OMG, that´s embarrassing 🫢

Thank you very much Imar - that solves it 👍

/Hans

 

You must be logged in to post in the forum