[英]WooCommerce Rest API - adding product author to products list
I am using WooCommerce with WC Vendors plugin.我正在使用 WooCommerce 和 WC Vendors 插件。 I am trying to return vendor name and id when returning the product list through the API call ( /wp-json/wc/v3/products
)我试图在通过 API 调用返回产品列表时返回供应商名称和 ID ( /wp-json/wc/v3/products
)
I have been advised to use the following code in functions.php:我被建议在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.我得到的数据不包含 vendor_id 或 vendor_name。
Adding this code to your theme's functions.php file:将此代码添加到主题的 functions.php 文件中:
if ( post_type_exists( 'product' ) ) {
add_post_type_support( 'product', 'author' );
}
Hope it helps you out!希望能帮到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.