Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active April 3, 2023 03:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogists/28114c936874719f8dcb698f8c4bacc6 to your computer and use it in GitHub Desktop.
Save woogists/28114c936874719f8dcb698f8c4bacc6 to your computer and use it in GitHub Desktop.
Brands REST API

Brands REST API

The Brands REST API allows you to create, view, update, and delete individual, or a batch, of brands. The endpoint is /wp-json/wc/v1/products/brands which basically mimics /wp-json/wc/v1/products/categories. You can refer to the same documentation of product categories REST API.

In addition to /products/brands endpoints, the /products endpoints also updated to display brands in the response and check the JSON request for brands.

Examples

  • Retrieve all product brands:

    curl https://example.com/wp-json/wc/v2/products/brands -u consumer_key:consumer_secret
    
  • Create a product brand:

    curl -X POST https://example.com/wp-json/wc/v2/products/brands \
     -u consumer_key:consumer_secret \
     -H "Content-Type: application/json" \
     -d '{
     "name": "Apple",
     "image": {
       "src": "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Apple_logo_black.svg/768px-Apple_logo_black.svg.png"
      }
    }'
    
  • Delete a product brand:

    curl -X DELETE https://example.com/wp-json/wc/v2/products/brands/9?force=true -u consumer_key:consumer_secret
    
  • Set brands to a product:

     curl -X PUT https://example.com/wp-json/wc/v2/products/123 \
      -u consumer_key:consumer_secret \
      -H 'Content-Type: application/json' \
      -d '{"brands": [48, 49]}'
    

    Note: When setting a brand to a product the url needs to be products/123 where 123 is the id of the product you want to update.

@mukyz
Copy link

mukyz commented May 30, 2019

Why not use {"brands": [ {"id": 48}, {"id": 49} ]} to set brands, the same way you set categories. It took me a while to find this page and see that the standard has changed

@Bjornhall
Copy link

Can you list all products associated to a specific brand through the api?

Such as https://example.com/wp-json/wc/v2/products?brand_id=48

@mattepuffo
Copy link

hi!

what about the v3 of the API?

@WebTekInternalTeam
Copy link

{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method.",
"data": {
"status": 404
}
}

@jeroenlammerts
Copy link

jeroenlammerts commented Apr 7, 2021

Why not use {"brands": [ {"id": 48}, {"id": 49} ]} to set brands, the same way you set categories. It took me a while to find this page and see that the standard has changed

Exactly, it does not work the same as categories.
Can this be mentioned in the docs?

@solstudioim
Copy link

{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method.",
"data": {
"status": 404
}
}

This should work: https://example/wp-json/wc/v3/products/brands?consumer_key=123&consumer_secret=abc

@thetahmeed
Copy link

{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method.",
"data": {
"status": 404
}
}

This should work: https://example/wp-json/wc/v3/products/brands?consumer_key=123&consumer_secret=abc

Still getting the same error

@moatazfouad
Copy link

{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method.",
"data": {
"status": 404
}
}

This should work: https://example/wp-json/wc/v3/products/brands?consumer_key=123&consumer_secret=abc

Still getting the same error

@thetahmeed is there any updates about that? facing same issue

@Mshimaz
Copy link

Mshimaz commented Sep 9, 2022

{
"code": "rest_no_route",
"message": "No route was found matching the URL and request method.",
"data": {
"status": 404
}
}

This should work: https://example/wp-json/wc/v3/products/brands?consumer_key=123&consumer_secret=abc

Still getting the same error

@thetahmeed is there any updates about that? facing same issue

is there any solution?

@nekranox
Copy link

nekranox commented Feb 24, 2023

This does not explain how to remove brands from a product.

I tried this and it did not work

curl -X PUT https://example.com/wp-json/wc/v2/products/123 \ -u consumer_key:consumer_secret \ -H 'Content-Type: application/json' \ -d '{"brands": []}'

Also this did not work

curl -X PUT https://example.com/wp-json/wc/v2/products/123 \ -u consumer_key:consumer_secret \ -H 'Content-Type: application/json' \ -d '{"brands": null}'

In both cases a brand that was previously assigned to the product remained so.

UPDATE: This does work
curl -X PUT https://example.com/wp-json/wc/v2/products/123 \ -u consumer_key:consumer_secret \ -H 'Content-Type: application/json' \ -d '{"brands": [0]}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment