简体   繁体   中英

Accessing base nop model custom properties from a product id in nopCommerce

I'm trying to access the custom properties of a product I have fetched by product id, but I really don't know how to go about this.

So if I grab the product like so:

var product = _productService.GetProductByIds(productId);

I would like to then access these properties with if statement:

product.CustomProperties.Keys.Contains("Popular")   

Can't seem to find anything on this, can anyone help?

First of all, GetProductById s return a list of products, and perhaps you intend to get a single product only. Thus, use GetProductById instead.

Also, there is nothing like "Popular" inside product model, possibly you'd have added custom one.

Finally, I assume that you'd like to check if the property exists within the product model or not! that can be done as follows,

var product = _productService.GetProductById(productId);
var productProperty =  product.GetType().GetProperty("Sku");
if(productProperty != null)
{
    //exists
}
else
{
    //doesn't exists
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM