ResolvedLafka y Woo API REST
Tagged: API REST
- This topic has 4 replies, 3 voices, and was last updated 1 year, 1 month ago by
jose.
- AuthorPosts
- September 30, 2022 at 9:42 pm #35116
jose
ParticipantI 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?
October 4, 2022 at 10:20 am #35155Althemist
KeymasterHello jose,
Sorry for the late reply. I’ll ask our lead developer Alex to step-in and give you information on this.
October 4, 2022 at 10:22 am #35157jose
ParticipantOk, I wait for Alex’s answer
October 6, 2022 at 5:39 pm #35377Alex
KeymasterHello 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,
AlexOctober 6, 2022 at 7:29 pm #35380jose
ParticipantThank’s
- AuthorPosts
You must be logged in and have valid license to reply to this topic.