0

There can be a lot of custom attributes for a product and some have very long values. Is there a way to filter the responses to include just one specific attribute_code value?

Note: this is not talking about filtering the Products returned, I have other criteria to filter the top level products, I just don't want to return the full custom_attributes array, but rather something like this in SQL SELECT * FROM custom_attributes WHERE attribute_code = 'some value'

1

Figured out how to do this.

It seems Magento treats the custom_attributes collection properties (attribute_codes) as field names directly, so you can search by them and limit their results in the 'fieldslist by referring to theattribute_code` value directly.

Let's take a look at the following attribute_code within custom_attributes example:

{
   "attribute_code": "color",
   "value": "red"
}

To filter the products to include only those that have a custom_attribute of color = red, you can do this:

searchCriteria[filter_groups][0][filters][0][field]=color
&searchCriteria[filter_groups][0][filters][0][value]=red
&searchCriteria[filter_groups][0][filters][0][condition_type]=eq

And further, to limit the collection of custom_attributes to only return the color custom attribute, include it in the list of fields, like this (specifically the custom_attributes[color] part, but showing it within the context of including some other fields as you normally would):

items[id,sku,name,custom_attributes[color]]

If you want more than one attribute, list them via a comma separated list, like so:

custom_attributes[color,size]

The only strange thing about this, is that it is wrapping the attribute_code within a higher level parent, 12 in this case...which I think is the Attribute Id.

"custom_attributes": {
   "12": {
            "attribute_code": "ship_length",
            "value": "11.0000"
         }
    }

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.