• Resolved jnz31

    (@jnz31)


    hi. i use this plugin for simply getting products via wp-json api. but when i try to filter products, say by attribute, i always get all products, no filter is applied:
    /wp-json/cocart/v2/products?attribute=pa_finish&attribute_term=1098
    /wp-json/cocart/v2/products?pa_finish=1098
    all return the same results (getting over 400 products in return, expecting 1 or so)

    /wp-json/cocart/v2/products?category=429 works just fine

    am i doing this correct?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Sébastien Dumont

    (@sebd86)

    Hi @jnz31

    First of all what happened to the other ticket? I didn’t get a chance to respond.

    Second. Have you made sure you are passing the correct attribute slug? What happens if you only apply one of the attribute variables?

    Thread Starter jnz31

    (@jnz31)

    hi @sebd86
    thanks for the fast reply.
    1. i modified the other ticket too often and it is now blocked for revision.

    2. yes, the url should be fine, attribute is called finish and the slug (i copied it from the url of the taxonomy term) is pa_finish, the id exists as well..

    3. if i modify the url to only ?attribute=pa_finish or ?attribute_term=xxx its the same result

    • This reply was modified 2 years, 7 months ago by jnz31.
    Plugin Author Sébastien Dumont

    (@sebd86)

    OK so I’m currently updating the API reference to make things clearer it’s just taken longer than I thought but in order to query products via attributes you have to do it like so.

    wp-json/cocart/v2/products?attributes[0][attribute]=pa_color&attributes[0][slug]=red

    This allows you to filter by many attributes so attributes[0] represents the colour attribute and then what you can do is append another to filter by size.

    attributes[1][attribute]=pa_size&attributes[1][slug]=small

    So in all you could query the following:

    wp-json/cocart/v2/products?attributes[0][attribute]=pa_color&attributes[0][slug]=red&attributes[1][attribute]=pa_size&attributes[1][slug]=small

    Hope that helps.

    Thread Starter jnz31

    (@jnz31)

    jaman, that totally helps and works! thanks. wicked, as it makes it super easy to query for multiple attributes!! ??

    Plugin Author Sébastien Dumont

    (@sebd86)

    I’m glad. Until the new API reference is available you can make an OPTIONS request for the products API to get all the arguments and values that you are allowed to pass to filter the products. Happy coding.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘trying to filter products by attribute’ is closed to new replies.