Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Register taxonomy API for WC
- add_action( 'rest_api_init', 'register_rest_field_for_custom_taxonomy_brands' );
- function register_rest_field_for_custom_taxonomy_brands() {
- register_rest_field('product', "marca", array(
- 'get_callback' => 'product_get_callback',
- 'update_callback' => 'product_update_callback',
- 'schema' => null,
- ));
- }
- //Get Taxonomy record in wc REST API
- function product_get_callback($post, $attr, $request, $object_type)
- {
- $terms = array();
- // Get terms
- foreach (wp_get_post_terms( $post[ 'id' ],'marca') as $term) {
- $terms[] = array(
- 'id' => $term->term_id,
- 'name' => $term->name,
- 'slug' => $term->slug
- );
- }
- return $terms;
- }
- //Update Taxonomy record in wc REST API
- function product_update_callback($values, $post, $attr, $request, $object_type)
- {
- // Post ID
- $postId = $post->ID();
- //Example: $values = [2,4,3];
- // Set terms
- wp_set_object_terms( $postId, $values , 'marca');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement