5

I have custom multiselect attribute and want to get selected values on Product List Page.

If attribute has used_in_product_listing=1, I can get only string of ids on Product List Page (something like $_product->getCustomMultAttr() == '236,238,239').

Could somebody tell me how to save in flat table values of this attribute, but not ids?

1 Answer 1

3

I think what you are really looking for is a way of printing in your template the attribute value labels corresponding to those comma-separated IDs. I see no point storing the labels themselves in the database flat tables, as this will require significant overhead whenever an attribute value label is changed in Admin: you would need to have code that iterates over all occurrences of that value ID (say 239) and update all related flat tables accordingly. Also, if you amended only the flat tables, this would cause issues when flat tables are disabled for any reasons.

You can include the labels in your template either by using:

echo $_product->getResource()->getAttribute('custom_mult_attr')->getFrontend()->getValue($_product);

or by splitting the comma-separated list of IDs and fetching the correct store value label for each, for example by using something like:

if (($attr = $_product->getResource()->getAttribute('custom_mult_attr')) && $attr->usesSource()) {
    if ($ids = explode(',', $_product->getData('custom_mult_attr'))) {
        foreach ($ids as $id) {
            echo $attr->getSource()->getOptionText($id);
        }
    }
}

This - of course - is just an example, the actual code may have to be amended to suit your needs, but it should give you an idea.

Should you still want to store the labels in the flat tables, I'd suggest you create another attribute of type text, and populate/update it on product or attribute save with the label values obtained as per the examples above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.