Search code examples
phpwordpresswoocommercehook-woocommercewoocommerce-rest-api

Make a Product Custom Taxonomy available in WooCommerce REST API


I have created custom taxonomy "Brands" 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 for that?


Solution

  • 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 in plugin.

    Step:1 Replace brands with your taxonomy_name.

    Step:2 If your taxonomy have custom fields, replace custom_field_name with yours.

    Taxonomy Code:

    // create a custom taxonomy name it topics for your posts
    add_action( 'init', 'create_brands_hierarchical_taxonomy', 0 );
    function create_brands_hierarchical_taxonomy() {
        // Add new taxonomy, make it hierarchical like categories
        // first do the translations part for GUI
         
        $labels = array(
            'name' => _x( 'Brands', 'taxonomy general name' ),
            'singular_name' => _x( 'Brand', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Brands' ),
            'all_items' => __( 'All Brands' ),
            'parent_item' => __( 'Parent Brand' ),
            'parent_item_colon' => __( 'Parent Brand:' ),
            'edit_item' => __( 'Edit Brand' ), 
            'update_item' => __( 'Update Brand' ),
            'add_new_item' => __( 'Add New Brand' ),
            'new_item_name' => __( 'New Brand Name' ),
            'menu_name' => __( 'Brands' ),
        );  
    
        $capabilities = array(
            'manage_terms'               => 'manage_woocommerce',
            'edit_terms'                 => 'manage_woocommerce',
            'delete_terms'               => 'manage_woocommerce',
            'assign_terms'               => 'manage_woocommerce',
        );
         
        $args = array(
            'labels'                     => $labels,
            'show_in_rest'               => true,       
            'hierarchical'               => true,
            'public'                     => true,
            'show_ui'                    => true,
            'show_admin_column'          => false,
            'show_in_nav_menus'          => true,
            'show_tagcloud'              => true,
            'capabilities'               => $capabilities,
        );
    
        // Now register the taxonomy
        register_taxonomy( 'brands', array( 'product' ), $args );
        register_taxonomy_for_object_type( 'brands', 'product' );
    }
    

    Register Taxonomy API for WC

    // 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', "brands", 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' ],'brands') as $term) {
            $terms[] = array(
                'id'        => $term->term_id,
                'name'      => $term->name,
                'slug'      => $term->slug,
                'custom_field_name'  => get_term_meta($term->term_id, 'custom_field_name', true)
            );
        }
    
        return $terms;
    }
            
    // Update Taxonomy record in wc REST API
    function product_update_callback($values, $post, $attr, $request, $object_type)
    {   
        // Example of term IDs values: $values = [2,4,3];                
        
        // Set terms
        wp_set_object_terms( $post->ID, $values , 'brands');
    }