Developer forum

Forum » Development » Get CategoryfieldValues of type Property

Get CategoryfieldValues of type Property

Aki Ruuskanen
Aki Ruuskanen
Reply

Hi,

I need to do some custom stuff with variant product that have propertyfields of type "Checkboxlist". I am trying to figure out how to get these values. 

I know I can use this to get the value:

var pvalue = productService.GetPropertyValue(variantProduct, "NameOfProperty");

But how do I get which properties a product have? 

The variantProduct.GetCategories() returns only categories of type "Category", not "Property".

Regards / Aki

 

property.png

Replies

 
Aki Ruuskanen
Aki Ruuskanen
Reply

To be more clear perhaps.

Is it possible to get a collection of property fields that a product have?

Regards / Aki

 
Alexander Gubenko
Alexander Gubenko
Reply

Hello Aki Ruuskanen

I think you can use this sample to get product properties and their values.

    public class ProductCategoryPropertyHelper
    {
        public Dictionary<ProductField, object> GetAllProprtiesValues(string productId, string variantId, string languageId)
        {
            var result = new Dictionary<ProductField, object>();
            var product = Dynamicweb.Ecommerce.Services.Products.GetProductById(productId, variantId, languageId);
            var productProperties = GetProductProperties(product);
            foreach (var field in productProperties)
            {
                var propValue = Dynamicweb.Ecommerce.Services.Products.GetFieldValue(product, field);
                if (propValue.Succeeded && propValue.Data != null)
                {
                    result.Add(field, propValue.Data);
                }
            }
            return result;
        }

        private IEnumerable<ProductField> GetProductProperties(Product product)
        {
            var productPropertyCategories = Dynamicweb.Ecommerce.Services.ProductCategories
                .GetCategories(product, true)
                .Where((category) => category.CategoryType == Dynamicweb.Ecommerce.Products.Categories.CategoryType.PropertyFields);
            var propertyFieldsCategories = productPropertyCategories
                .SelectMany(category => category.Fields)
                .Select(field => $"ProductCategory|{field.Category.Id}|{field.Id}");
            var systemNamesIdx = new HashSet<string>(propertyFieldsCategories);
            var result = ProductField.GetCategoryFields().Where(field => systemNamesIdx.Contains(field.SystemName)).ToList();
            return result;
        }
    }

BR, Alexander

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Thanks Alexander. I will try it.

I ended up getting my data with SQL but it would of course be better with the API.

Regards / Aki

 

 

You must be logged in to post in the forum