I have created custom taxonomy "Brands" and attached to woocommerce products. Unfortunately, It is not available in woocommerce REST API response.
How to attach custom taxonomy term to woocommerce rest API. Currently there is no documentation about attachment of custom taxonomy. Is there any hook or filter ?
Question posted in Woocommerce
The official Woocommerce documentation can be found here.
The official Woocommerce documentation can be found here.
5
Answers
I solved by using this code.You will be able to GET and Update custom taxonomy by using this method. Basically. You can add this code in
functions.php
file or inplugin
.Step:1 Replace
brands
with yourtaxonomy_name
.Step:2 If your taxonomy have custom fields, replace
custom_field_name
with yours.Taxonomy Code:
Register Taxonomy API for WC
Solution above is working but you have to change the
with
Took me ages to find out why update_callback did not work : $values contains the taxonomy-structure and is therefore too complex to pass as integer to wp_set_object_terms. You have to strip it to the numeric ID’s only otherwise [null] is assigned
If anyone has tested all the above and can’t get it to work, I found a simplest and working solution, based on Taha Farooqui’s answer and the wordpress register_rest_field documentation :
After months of testing trying to prove why the product_update_callback() function returned a null value, he got a version that loads the taxonomy values from the woocommerce Rest Api v3 only with the name and slug..
After so much testing he understood that the values that pass through the product_get_callback_brand function are mandatory to register a taxonomy so if you add a custom field like in the first example then they must be loaded so that the taxonomy can be retrieved.
It is worth noting that it is not a valid field for image fields or galleries that will not have the permissions to be loaded by the Rest Api v3 of woocommerce