我有以下脚本,该脚本用于带有自定义端点的woocommerce rest api。在响应中传递了供应商ID以及供应商地址和地址编码。但是,距离值不是。当我使用wp-json / wc / v2 / products时会发生这种情况,但是如果我将字符串更改为wp-json / wc / v1 / products,那么我会收到所有响应中的值。
public function get_vendor_product($response, $post, $request){
if(isset($request['is_vendor']) && $request['is_vendor']==true){
$vendor_id=$request['vendor_id'];
$shop_name=WCV_Vendors::get_vendor_shop_name($vendor_id);
$shop_desc=get_user_meta( $vendor_id, 'pv_shop_description',true);
$myLat = $request['myLat'];
$myLng = $request['myLng'];
$user_location=get_user_meta($post->post_author,'sow_vendor_geocoding',true);
if($user_location){
$user_location_array=split(',',$user_location);
$distance = \GeometryLibrary\SphericalUtil::computeDistanceBetween(
['lat' => $myLat, 'lng' => $myLng],
['lat' => $user_location_array[0], 'lng' => $user_location_array[1]]);
}
$distance = round($distance/1000, 2)."km";
$response->data['distance']= $distance;
$response->data['shop_name']= $shop_name;
$response->data['shop_desc']= $shop_desc;
$response->data['vendor_id']= $request['vendor_id'];
$response->data['vendor_address']= get_user_meta($vendor_id,'sow_vendor_address',true);
$response->data['vendor_geocoding']= get_user_meta($vendor_id,'sow_vendor_geocoding',true);
return $response;
}
对于过滤器,我使用以下代码:
add_filter( 'woocommerce_rest_prepare_product', array( $this, 'get_vendor_product' ),10,3);