Woocommerce API Custom Taxonomies and Meta Fields

Woocommerce API Custom Taxonomies and Meta

My Ecommerce project requires an import/export task between the new site on woocommerce and the old one. Woocommerce has provided an excellent API for all the stuff you might need. Thank you very much Woocommerce and Wordpress.

(I planned to buy this Woocommerce CSV importer plugin with its add-ons but I found that Woocommerce API provided me everything. I need to spend a little bit more time to save $70 :D).

However, Woo V3 API doesn’t provide product custom taxonomies and meta fields when creating/updating/showing products. So you need to implement this feature within your plugin. Here is how I did it:

About my project:

I need to add custom taxonomies to the product including: Brand, Collection, Audience taxonomies. I also need to add 2 meta fields including: Product Color Swatch and Product’s related SKUs.

Solution:

1/ You can put these functions into theme’s functions.php or inside your own plugin. I put these in my plugins for the current project.

2/ As you can find in the class-wp-api-products.php of Woocommerce, there are several filters and actions for modifying/showing product info:

On line 193, there is a filter for showing additional info of products:

    return array( 'product' => apply_filters( 'woocommerce_api_product_response', $product_data, $product, $fields, $this->server ) );

On line 280 of this file, you can find the action hook for saving more information of the product when creating:

    do_action( 'woocommerce_api_create_product', $id, $data );

On line 398, there is an action hook for product editing:

    do_action( 'woocommerce_api_edit_product', $id, $data );

3/ So, you only need to hook into these actions/filters. Here is the code

function custom_api_product_response($data, $product, $fields, $server){
    // return $data;
    $data['_color_sw'] = get_post_meta( $product->post->ID, '_color_sw'); ;
    $data['_related_skus'] = get_post_meta( $product->post->ID, '_related_skus');
    $data['brand'] = wp_get_post_terms($product->post->ID, 'brand');
    $data['collection'] = wp_get_post_terms($product->post->ID, 'collection');
    $data['audience'] = wp_get_post_terms($product->post->ID, 'audience');
    $data['category'] = wp_get_post_terms($product->post->ID, 'product_cat');
    return $data;
}

add_filter( 'woocommerce_api_product_response', 'custom_api_product_response', 10, 2 );

function custom_add_more_fields_to_product_api ($product_id, $data) {
    // Add brand erms
    if ( isset( $data['brands'] ) && is_array( $data['brands'] ) ) {
        $term_ids = array_unique( array_map( 'intval', $data['brands'] ) );
        wp_set_object_terms( $product_id, $term_ids, 'brand' );
    }

    // Add collection terms
    if ( isset( $data['collections'] ) && is_array( $data['collections'] ) ) {
        $term_ids = array_unique( array_map( 'intval', $data['collections'] ) );
        wp_set_object_terms( $product_id, $term_ids, 'collection' );
    }

    // Add audience terms
    if ( isset( $data['audiences'] ) && is_array( $data['audiences'] ) ) {
        $term_ids = array_unique( array_map( 'intval', $data['audiences'] ) );
        wp_set_object_terms( $product_id, $term_ids, 'audience' );
    }

    // Add meta values

    // Product Color Swatch
    if ( isset( $data['color_sw'] ) ) {
        update_post_meta( $product_id, '_color_sw', wc_clean( $data['color_sw'] ) );
    }

    // Product Related Skus
    if ( isset( $data['related_skus'] ) ) {
        update_post_meta( $product_id, '_related_skus', wc_clean( $data['related_skus'] ) );
    }
}
// Add Action to Product Variable
add_action( 'woocommerce_api_create_product',  'custom_add_more_fields_to_product_api', 10, 2);
add_action( 'woocommerce_api_edit_product',  'custom_add_more_fields_to_product_api', 10, 2);

Hope this helps. Happy Coding!

Written on September 25, 2015