ResolvedLafka y Woo API REST

Tagged: 

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #35116
    jose
    Participant

    I am using Woocommerce API REST, and when making product calls I need to be able to use the custom taxonomy “lafka_branch_location” to see and change the restaurants where the product is available.

    I have tried this:
    add_filter( ‘woocommerce_rest_prepare_product_object’, ‘wc_rest_api_add_custom_data_to_product’, 100, 3 );
    function wc_rest_api_add_custom_data_to_product( $response, $object, $request ) {
    $response-> data[‘branch’] = get_term_meta( $item->term_id, ‘lafka_branch_location’, true );
    return $response;
    }

    but it returns an empty array, could you ask your developers, how should the function be?

    #35155
    Althemist
    Keymaster

    Hello jose,

    Sorry for the late reply. I’ll ask our lead developer Alex to step-in and give you information on this.

    #35157
    jose
    Participant

    Ok, I wait for Alex’s answer

    #35377
    Alex
    Keymaster

    Hello Jose,

    The problem is in your function. You are trying to get the term meta and you don’t have term in the filter parameters. Here is the right code.
    We are getting all terms for lafka_branch_location (All branches where the product is set) and we are constructing array with its ids and append it to the response.
    You can change the code to get the names of the branches instead. It’s up to you.

    
    add_filter( ‘woocommerce_rest_prepare_product_object’, ‘wc_rest_api_add_custom_data_to_product’, 100, 3 );
    function wc_rest_api_add_custom_data_to_product( $response, $product, $request ) {
        $term_objects = get_the_terms($product->get_id(), 'lafka_branch_location');
        $term_ids = array();
        if($term_objects !== false) {
    	    foreach ( $term_objects as $object ) {
    		    $term_ids[] = $object->term_id;  // For names use: $object->name;
    	    }
        }
        $response-> data['branch'] =$term_ids;
    return $response;
    }
    

    Regards,
    Alex

    #35380
    jose
    Participant

    Thank’s

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in and have valid license to reply to this topic.

License required for the following item
Login and Registration Log in · Register