Advertisement
vazcooo

custom php

Jan 24th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. //Register taxonomy API for WC
  2. add_action( 'rest_api_init', 'register_rest_field_for_custom_taxonomy_brands' );
  3. function register_rest_field_for_custom_taxonomy_brands() {
  4.    
  5.  
  6.     register_rest_field('product', "marca", array(
  7.         'get_callback'    => 'product_get_callback',
  8.         'update_callback'    => 'product_update_callback',
  9.         'schema' => null,
  10.     ));    
  11.  
  12. }
  13.         //Get Taxonomy record in wc REST API
  14.          function product_get_callback($post, $attr, $request, $object_type)
  15.         {
  16.             $terms = array();
  17.  
  18.             // Get terms
  19.             foreach (wp_get_post_terms( $post[ 'id' ],'marca') as $term) {
  20.                 $terms[] = array(
  21.                     'id'        => $term->term_id,
  22.                     'name'      => $term->name,
  23.                     'slug'      => $term->slug
  24.                 );
  25.             }
  26.  
  27.             return $terms;
  28.         }
  29.        
  30.          //Update Taxonomy record in wc REST API
  31.          function product_update_callback($values, $post, $attr, $request, $object_type)
  32.         {  
  33.             // Post ID
  34.             $postId = $post->ID();
  35.            
  36.             //Example: $values = [2,4,3];                
  37.            
  38.             // Set terms
  39.            wp_set_object_terms( $postId, $values , 'marca');
  40.            
  41.            
  42.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement