简体   繁体   English

WP Rest API v2 自定义端点

[英]WP Rest API v2 custom endpoints

I'm using wp rest api to feed woocommerce products in my ionic2 app.我正在使用 wp rest api 在我的 ionic2 应用程序中提供 woocommerce 产品。 So, I would like to feed only products with the in_stock parameter on true.因此,我只想提供in_stock参数为 true 的产品。 My Plugin, which communicate between rest api and mobile app is using version 1 of wp-json/wc/v1/products but the version 1 is not delivering the in_stock parameter, that's why I would like to use wp-json/wc/v2/products .我的插件,在 rest api 和移动应用程序之间进行通信使用wp-json/wc/v1/products的第 1 版,但第 1 版没有提供in_stock参数,这就是我想使用wp-json/wc/v2/products This works also without any issue.这也没有任何问题。 However, I miss some data in my json response.但是,我在 json 响应中遗漏了一些数据。 The following function in my plugin add the geodata and address to the response我的插件中的以下函数将地理数据和地址添加到响应中

add_filter( 'woocommerce_rest_prepare_product', array( $this, 'get_vendor_product' ),10,3);

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);

        $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);
    }else if(isset($request['id']) && WCV_Vendors::is_vendor($post->post_author)){
        $response->data['vendor_id']= $post->post_author;
        $shop_name=WCV_Vendors::get_vendor_shop_name($post->post_author);
        $shop_desc=get_user_meta( $post->post_author, 'pv_shop_description',true);

        $response->data['shop_name']= $shop_name;
        $response->data['shop_desc']= $shop_desc;
        $response->data['vendor_address']= get_user_meta($post->post_author,'sow_vendor_address',true);
        $response->data['vendor_geocoding']=  get_user_meta($post->post_author,'sow_vendor_geocoding',true);
    }else{
        if(WCV_Vendors::is_vendor($post->post_author)){
            $response->data['vendor_address']= get_user_meta($post->post_author,'sow_vendor_address',true);
            $response->data['vendor_geocoding']=  get_user_meta($post->post_author,'sow_vendor_geocoding',true);
        }
    }
    return $response;
}

I could not identify, why when I use version 2 (wp-json/wc/v2/products) the geo data is not in the response.我无法确定为什么当我使用版本 2 (wp-json/wc/v2/products) 时,地理数据不在响应中。 Does the version 2 use other endpoints?版本 2 是否使用其他端点?

You can try this one by the condition "in_stock" as true but the issue is that if you will not return anything when "in_stock" is false then woocommerce automatically return product response as null.您可以通过条件“in_stock”为真来尝试这个,但问题是,如果当“in_stock”为假时您不会返回任何内容,那么woocommerce会自动将产品响应返回为空。

You can be modified with the blank array instead of null by else condition.您可以通过 else 条件使用空白数组而不是 null 进行修改。

add_filter( 'woocommerce_rest_prepare_product', array( $this, 'get_vendor_product' ),10,3);

public function get_vendor_product($response, $post, $request){

if( $response->data['in_stock'] === true ) {

    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);

        $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);
    }else if(isset($request['id']) && WCV_Vendors::is_vendor($post->post_author)){
        $response->data['vendor_id']= $post->post_author;
        $shop_name=WCV_Vendors::get_vendor_shop_name($post->post_author);
        $shop_desc=get_user_meta( $post->post_author, 'pv_shop_description',true);

        $response->data['shop_name']= $shop_name;
        $response->data['shop_desc']= $shop_desc;
        $response->data['vendor_address']= get_user_meta($post->post_author,'sow_vendor_address',true);
$response->data['vendor_geocoding']=  get_user_meta($post->post_author,'sow_vendor_geocoding',true);
    }else{
        if(WCV_Vendors::is_vendor($post->post_author)){
        $response->data['vendor_address']= get_user_meta($post->post_author,'sow_vendor_address',true);
        $response->data['vendor_geocoding']=  get_user_meta($post->post_author,'sow_vendor_geocoding',true);
        }

    }
    return $response;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM