简体   繁体   中英

WooCommerce Rest API - adding product author to products list

I am using WooCommerce with WC Vendors plugin. I am trying to return vendor name and id when returning the product list through the API call ( /wp-json/wc/v3/products )

I have been advised to use the following code in functions.php:

function filter_woocommerce_api_product_response( $product_data, $product, $fields, $this_server ) { 
    $product_data['vendor_id'] = get_post_field( 'post_author', $product->id);
    $product_data['vendor_name'] = get_the_author_meta( 'display_name', $product_data['vendor_id']);
    return $product_data; 
};      
add_filter( 'woocommerce_api_product_response', 'filter_woocommerce_api_product_response', 10, 4 ); 

The data I get does not contain either the vendor_id or vendor_name.

Adding this code to your theme's functions.php file:

if ( post_type_exists( 'product' ) ) {
add_post_type_support( 'product', 'author' );
}

Hope it helps you out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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