Specification attribute CustomValue inserted in database for Option AttributeType

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
This bug exists in nop 3.6.  I haven't tested it the latest version but I can't see any code changes on GitHub that relate to it so I'm assuming it exists there too.

After adding an attribute of type Custom HTML text, if another attribute of Option type is then added to the product the CustomValue text from the previously added attribute is also written to the CustomValue column of the second attribute mapping.

Steps to replicate
1. Go to the product edit page and add a specification attribute to type Custom HTML text to the product
2. Change the attribute type dropdown to Option (without first clearing the text in the Custom Value field)
3. Add a second attribute of the Option type to the product.
4. Run a query against the database to find attribute mappings in the Product_SpecificationAttribute_Mapping table that have an AttributeTypeId = 0 and CustomValue <> ''
(I'd post the SQL for this but Cloudflare keeps blocking it as part of their security protection)

Under normal circumstances you wouldn't expect that query to return any results since the CustomValue column shouldn't apply to Option attributes but I'm seeing a lot of results in my database where option values have been added after custom html text values. This will also happen if the first attribute was custom text or hyperlink attribute type too.

I think this could be fixed in one of two ways:
1. Check the AttributeTypeId in the ProductSpecificationAttributeAdd ActionResult of the Administration ProductController.cs and set the CustomValue to an empty string if it's 0. e.g.

(addition in bold)
//we allow filtering only for "Option" attribute type
if (attributeTypeId != (int)SpecificationAttributeType.Option)
{
    allowFiltering = false;
}

//we don't allow CustomValue for "Option" attribute type
if (attributeTypeId == (int)SpecificationAttributeType.Option)
{
    customValue = string.Empty;
}


var psa = new ProductSpecificationAttribute
{
    AttributeTypeId = attributeTypeId,
    SpecificationAttributeOptionId = specificationAttributeOptionId,
    ProductId = productId,
    CustomValue = customValue,
    AllowFiltering = allowFiltering,
    ShowOnProductPage = showOnProductPage,
    DisplayOrder = displayOrder,
};


2. In the toggleAttributeType() function of the _CreateOrUpdate.SpecificationAttributes.cshtml view make sure the contents of the CustomValue field are blanked when changing to to the Option AttributeType. e.g.

(addition in bold)
function toggleAttributeType() {
    var selectedTypeId = $("#@Html.FieldIdFor(model => model.AddSpecificationAttributeModel.AttributeTypeId)").val();
    if (selectedTypeId == @(((int)SpecificationAttributeType.Option).ToString())) {
        $('#pnlSpecificationAttributeOptionId').show();
        $('#pnlCustomValue').hide();
        $('#pnlAllowFiltering').show();
        $("#@Html.FieldIdFor(model => model.AddSpecificationAttributeModel.CustomValue)").val("");
    } else if (selectedTypeId == @(((int)SpecificationAttributeType.CustomText).ToString())) {
        $('#pnlSpecificationAttributeOptionId').hide();
        $('#pnlCustomValue').show();
       $('#pnlAllowFiltering').hide();
    } else if (selectedTypeId == @(((int)SpecificationAttributeType.CustomHtmlText).ToString())) {
        $('#pnlSpecificationAttributeOptionId').hide();
        $('#pnlCustomValue').show();
        $('#pnlAllowFiltering').hide();
    } else if (selectedTypeId == @(((int)SpecificationAttributeType.Hyperlink).ToString())) {
        $('#pnlSpecificationAttributeOptionId').hide();
        $('#pnlCustomValue').show();
        $('#pnlAllowFiltering').hide();
    }
}


Hope that makes sense.

P.S. It would be great if had some sort of highlight option on the forum post wysiwyg editor. The bold text doesn't make the changes very obvious in a code block. Even just changing the css to change the text colour or background colour of bolded text would be enough.
8 years ago
Hi Pete,

Thanks a lot! I've just fixed it.
7 years ago
Before changing code, I was looking around, and so far, this is the closest existing thread to what I'm looking form. So wanted to discuss.

When specifying a custom value, we should be able to also allow filtering and to choose an option.

For example, if the product attribute value is 7.5, we should be able to type it in, and also be able to filter by it (by choosing option "6 to 10", assuming that options are ranges like "1 - 5", "6 - 10", "11 - 15", etc.) That way, we get both, the specific value (which can get displayed in the product page), and being able to filter (by groupings).
7 years ago
zootie wrote:
Before changing code, I was looking around, and so far, this is the closest existing thread to what I'm looking form. So wanted to discuss.

When specifying a custom value, we should be able to also allow filtering and to choose an option.

For example, if the product attribute value is 7.5, we should be able to type it in, and also be able to filter by it (by choosing option "6 to 10", assuming that options are ranges like "1 - 5", "6 - 10", "11 - 15", etc.) That way, we get both, the specific value (which can get displayed in the product page), and being able to filter (by groupings).

Hi Zootie. I've played around with enabling the behaviour you describe in the past. When you change a Specification attribute value to the custom text or custom html type it still gets mapped to an option value for that attribute in the background. The option value it's mapped to is whichever one was selected in the dropdown when you change it to the custom value type (and the dropdown for the option then gets hidden in the GUI so you can't change it easily, along with the option to allow filtering).

At this point if you save the attribute mapping and go and find it in the Product_SpecificationAttribute_Mapping table in the database you'll see that the AllowFiltering bit column is set to 0. If you manually change it to 1 then it actually enables the behaviour you describe where you can have an option value that is used for filtering and a different custom text (or html) value that is displayed on the product page.

While the efficiency of this appeals to me as a developer I decided it might actually be confusing to our product managers (the idea of maintaining separate filter and display values on the same attribute value mapping) so I decided to just add multiple attribute mappings where we wanted this kind of behaviour. So we add an option value attribute and set it to AllowFiltering: True and ShowOnProductPage: False, then add a second value for the same attribute of custom html type with AllowFiltering: False and ShowOnProductPage: True. The first one is only used in filtering and the second one only shows on the product page.

So yeah it's actually quite easy to get the behaviour you describe with only one attribute mapping just by not hiding the option dropdown and allow filtering checkbox in the GUI when a custom attribute type is selected, but it's also easy to do without modifications by mapping two attribute values: one for filtering and one for display.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.