Posted on 02/07/2024 12:37:29
Hi Nuno,
Of cause.
I wanted to select products for the component slider.
When I selected the 'Selected products' there was not an option to select any products.
So I added the group to the item so I got this options:
In the template it looked like this before:
ProductListViewModel relateToViewModel = Model.Item.GetValue<ProductListViewModel>("RelateTo");
//--- PRODUCTS ---
if (sourceType == "variants" || sourceType == "frequently" || sourceType == "selected" )
{
if (relateToViewModel?.Products != null)
{
hasVariants = relateToViewModel.Products.Any(p => !string.IsNullOrEmpty(p.VariantId));
foreach (var fromProduct in relateToViewModel.Products)
{
if (hasVariants)
{
if (!string.IsNullOrEmpty(fromProduct.VariantId))
{
relateFromProductVariantIds.Add($"{fromProduct.Id} {fromProduct.VariantId}");
}
else
{
relateFromProductVariantIds.Add($"{fromProduct.Id}");
}
}
relateFromProductIds.Add($"{fromProduct.Id}");
}
}
}
it gave an error in the frontend.
So I changed to this:
//--- PRODUCTS ---
if (sourceType == "variants" || sourceType == "frequently")
{
if (relateToViewModel?.Products != null)
{
hasVariants = relateToViewModel.Products.Any(p => !string.IsNullOrEmpty(p.VariantId));
foreach (var fromProduct in relateToViewModel.Products)
{
if (hasVariants)
{
if (!string.IsNullOrEmpty(fromProduct.VariantId))
{
relateFromProductVariantIds.Add($"{fromProduct.Id} {fromProduct.VariantId}");
}
else
{
relateFromProductVariantIds.Add($"{fromProduct.Id}");
}
}
relateFromProductIds.Add($"{fromProduct.Id}");
}
}
}
//--- SELECTED PRODUCTS ---
if ((sourceType == "selected") && Model.Item.GetValue<ProductListViewModel>("Products") is ProductListViewModel products)
{
hasVariants = products.Products.Any(p => !string.IsNullOrEmpty(p.VariantId));
foreach (var productSelection in products.Products)
{
if (hasVariants)
{
if (!string.IsNullOrEmpty(productSelection.VariantId))
{
relateFromProductVariantIds.Add($"{productSelection.Id} {productSelection.VariantId}");
}
else
{
relateFromProductVariantIds.Add($"{productSelection.Id}");
}
}
relateFromProductIds.Add($"{productSelection.Id}");
}
}