Posted on 29/06/2023 15:28:20
Hi,
I hope sample below will help
using Dynamicweb.Ecommerce.Products;
using System.IO;
using System.Linq;
namespace Dynamicweb.Ecommerce.Examples.Products
{
class ManageProductAssetsSample
{
void ManageDetails()
{
var product = Dynamicweb.Ecommerce.Services.Products.GetProductById("MyProduct", string.Empty, true);
var productAssets = Dynamicweb.Ecommerce.Services.Details.GetDetails(product);
var myAssetCategoryId = 1;
var myAssetCategory = Dynamicweb.Ecommerce.Services.DetailsGroups.GetById(myAssetCategoryId);
var extensions = Dynamicweb.Ecommerce.Services.DetailsGroups.GetDetailExtensions(); //taken from globalSettings "/Globalsettings/Ecom/Product/Details/Extensions"
foreach (var asset in productAssets)
{
if (asset.Name == "My name")
{
Dynamicweb.Ecommerce.Services.Details.UpdateDefaultDetailForProduct(product, asset.Name);
}
if (asset.Value.StartsWith("/Files/Images/My category/"))
{
Dynamicweb.Ecommerce.Services.Details.SetDetailsGroup(asset, myAssetCategory.Id);
}
var assetExtension = Path.GetExtension(asset.Value);
if (!extensions.Contains(assetExtension))
{
Dynamicweb.Ecommerce.Services.Details.Delete(asset.Id);
}
}
var newAsset = new Detail()
{
ProductId = product.Id,
VariantId = product.VariantId,
LanguageId = product.LanguageId,
Name = "Some label",
Keywords = "some key words",
Value = "/Files/Images/myProdImage.jpg"
};
Dynamicweb.Ecommerce.Services.Details.Save(newAsset, product);
}
}
}
Best Regards, Viktor.