vc-platform
vc-platform copied to clipboard
Simplify working with dynamic/catalog properties on the storefront
The main goal needs to simplify the process of working with Dynamic/Catalog properties from code through the definition of syntax sugar and fluent api.
Here is code demonstrates how to work with catalog properties from code
foreach (var record in planogramRecords)
{
var product = products.FirstOrDefault(x => x.Id == record.Product);
if (product != null)
{
var planogramProperty = product.Properties.FirstOrDefault(x => x.Name == "planogram");
if (planogramProperty != null)
{
var dictValues = _propertiesApi.GetPropertyValues(planogramProperty.Id);
var dictValue = dictValues?.FirstOrDefault(x => x.Value.ToString().EqualsInvariant(record.Planogram));
if (dictValue != null)
{
planogramProperty.Values = new productDto.PropertyValue[] { new productDto.PropertyValue { Value = record.Planogram, ValueId = dictValue.ValueId, PropertyName = "planogram" } };
}
}
}
}
await _productsApi.SaveProductsAsync(products.ToList());
And with the DynamicProperies
foreach (var store in stores)
{
var organization = new Organization { Id = store.Id, Name = store.Id, Description = store.Address };
if (store.Planogram != null)
{
var properties = await _dynamicPropertiesApi.GetPropertiesAsync("VirtoCommerce.Domain.Customer.Model.Organization");
var planogramProp = properties.FirstOrDefault(x => x.Name.EqualsInvariant("planogram"));
if (planogramProp != null)
{
var dictValues = await _dynamicPropertiesApi.GetDictionaryItemsAsync(planogramProp.ObjectType, planogramProp.Id);
var dictValue = dictValues.FirstOrDefault(x => x.Name.EqualsInvariant(store.Planogram));
if (dictValue != null)
{
var dynProp = planogramProp.JsonConvert<AutoRestClients.CoreModuleApi.Models.DynamicObjectProperty>().ToDynamicProperty();
dynProp.DictionaryValues = new[] { dictValue.ToDictItem() };
organization.DynamicProperties = new DynamicProperty[] { dynProp };
}
}
}
await _memberService.CreateOrganizationAsync(organization);