skip to Main Content

I have woo variation swatches plugin on my website and I want to use woocommerce and wordpress in a headless project. my problem is when I want to fetch attribute terms via

/wp-json/wc/v3/products/attributes/COLOR_ATTRIBUTE_ID/terms

the returned result doesn’t include the color code field. I know the color code is stored in termmeta table with product_attribute_color key. is there any way to add this meta as a field to the attributes result in rest API?

2

Answers


  1. Chosen as BEST ANSWER

    I posted the correct code for my own question, Lowin Ko Ko's answers helped me to find the solution but because his code is psuedo code I posted my code here.

    add_filter('woocommerce_rest_prepare_pa_color', 'add_color_meta', 10, 3);
    
    if(!function_exists('add_color_meta')) {
        function add_color_meta($response, $term) {
            $response->data['color_code'] = get_term_meta($term->term_id,'product_attribute_color');
            return $response;
        }
    }
    
    

  2. You can extend with woocommerce_rest_prepare_(taxonomy) hooks like this –

    add_filter( 'woocommerce_rest_prepare_pa_color', 'add_custom_data_to_product_attributes_terms', 10, 3 ); // where "taxonomy" = "pa_color" as taxonomy name
    
    // filter the product response here
    function add_custom_data_to_product_attributes_terms( $response, $post, $request ) {
        
        // $response->data['color-code'] = $response->data['description'];
        // execute all here
        
        return $response;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search