Developer forum

Forum » Ecommerce - Standard features » RE: Multi dropdown - custom product field value?

RE: Multi dropdown - custom product field value?

Mikkel Toustrup Olsen
Reply

Hello,

Im working on a solution, where I need to add several dropdowns with the ability to select multiple values. I am able to create the custom field, but not able to give the appropriate values a blank space in between, which means that all my values are being outputted without the "proper" space.

E.g. Car Parts, will be outputted as carparts, which isn't beneficial at all. Isn't there any way to output this in a more correct way without having to reformat the string etc.

/Mikkel


Replies

 
Mikkel Toustrup Olsen
Reply

Anyone? :-)

 
Adrian Ursu
Reply

Hi Mikkel,

It's not clear where you have the problem.

Is it backend or front-end?

It seems that you store the System values instead of the Name of the option.

One good Ideea would be to use "_" or "-" instead of trimming the space for the system values. This way it is more readable and you can process them back if you need the space.

 
Mikkel Toustrup Olsen
Reply

I am having the problem when outputting the Values from the multi-dropdown to the frontend..

In the backend I set up the dropdown as follow

Name - Value ----> Car Parts - carparts (im not allowed to make spaces in the value field) gives me an error.

Which means when I output the list on the frontend i get carparts. I can however insert e.g. Car_Parts as a value. Though i do have several options where special characters such as ( - / ) etc. are being used, though these are not allowed to set as values as well which gives me another problem :)

/Mikkel

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Yeah, this is a bit of an isue. For some reason Dynamicweb doesn't expose the labels, just the underlying values. I typically use one of the following two solutions:

- Use underscores in the value and then dislplay them using TagName.Replace('_', '')

- Write extensibility code targeting the API that retrieves the labels and makes them available in your page.

Cheers,

Imar

 
Mikkel Toustrup Olsen
Reply

Hey Imar,

Thanks for you reply.

I think the first solution, wit the TagName.Replace('_', ") would be my way to do it as it is. Do you though have an example of the last solution. I will sure look into this, if it's easy and kinda time-effective to come by, since that solution is "nicer" to me than the first :-)

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Mikkel,

Something like this would do the trick:

using System.Collections.Generic;
using System.Linq;
using Dynamicweb.eCommerce.Products.Categories;
using Dynamicweb.Extensibility;
using Dynamicweb.Rendering;

namespace Your.Project
{
  [AddInName("RenderProductFieldLabels")]
  public class RenderProductFieldLabels : TagExtensionMethod
  {
    public override string ExecuteMethod(string value)
    {
      // Get the ProductCategory
      string productCategoryName = Arguments[0].ToString();
      var productCategory = Category.GetCategoryByID(productCategoryName);
      if (productCategory == null) return value;

      // Get the FieldName
      string productCategoryFieldName = Arguments[1].ToString();
      var productFieldName = productCategory.Fields.FirstOrDefault(x => x.ID == productCategoryFieldName);
      if (productFieldName == null) return value;

      // Get the FieldOptions
      var availableOptions = productFieldName.FieldOptions;

      // Determine the separator that is placed between the items
      string separator = Arguments[2].ToString();

      // Get the selected options for this product
      var selectedOptions = new List<string>(value.Split(','));

      // Find the options in the available options that are selected and then grab their Name (which is the Label in the backend)
      var optionsList = availableOptions.Where(x => selectedOptions.Contains(x.Value)).Select(x => x.Name);

      return string.Join(separator, optionsList.ToArray());
    }
  }
}

 

This code expected three parameters: the system name of the product category, the system name of the individual field, and the separator that will be placed between items if you have a multi-select like a check box list. You can call it as follows:

<!--@Ecom:Product.CategoryField.CategorySystemName.FieldSystemName.Value.RenderProductFieldLabels("CategorySystemName", "FieldSystemName", "<br />")-->

This puts each item on a separate line. If you pass a comma you get a comma separated list. You can modify the code to render wrapping tags too, like an <li>

 

Hope this helps,

Imar

 

 

You must be logged in to post in the forum