有没有办法扩展 woocommerce Rest api 中的产品对象/wp-json/wc/v3/products
,以非破坏性的方式,因此使用该端点的插件不会中断。
我目前尝试创建自己的休息端点来复制该对象,但现在缺少大量数据。
我也尝试过类似的方法,但似乎没有效果。
add_filter( 'woocommerce_rest_prepare_product', 'wc_rest_api_add_custom_data_to_product', 90, 2 );
function wc_rest_api_add_custom_data_to_product( $response, $post ) {
// retrieve a custom field and add it to API response
$response->data['your_new_key'] = get_post_meta( $post->ID, '_your_custom_filed_key', true );
return $response;
}
Source: https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/ https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/
任何在这方面的帮助将不胜感激。
我找到了一种使用 register_rest_field 扩展端点的方法。
这样我就可以扩展(添加)和覆盖(更新)对象。
function register_images_field() {
register_rest_field(
'product',
'images',
array(
'get_callback' => function ( $object ) {
$medium_large = wp_get_attachment_image_src( get_post_thumbnail_id( $object['id'] ), 'medium_large' );
$medium_large_url = $medium_large['0'];
$large = wp_get_attachment_image_src( get_post_thumbnail_id( $object['id'] ), 'large' );
$large_url = $large['0'];
return array(
'medium_large' => $medium_large_url,
'large' => $large_url,
);
},
'update_callback' => null,
'schema' => null,
)
);
}
Source: https://developer.wordpress.org/reference/functions/register_rest_field/ https://developer.wordpress.org/reference/functions/register_rest_field/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)