【发布时间】:2019-01-27 10:40:16
【问题描述】:
我想扩展 woocommerce rest api 以包含其“预订”扩展插件的数据。目前这个扩展没有由 rest api 提供的默认端点。
到目前为止,我已经创建了一个插件,并添加了以下代码;
add_filter( 'woocommerce_rest_prepare_product', 'custom_data');
function custom_data($response, $object) {
if( empty( $response->data ) )
return $response;
$response->data['meta_data'] = get_post_meta( $object[ID], 'availability', true);
return $response;
}
当我调用端点/products 时,只有 woocommerce 概述的默认数据仍被称为我的小插件,找不到。
我什至不知道在哪里可以找到上面的过滤器,因为我刚刚在网页上看到这个帖子,我试图让它做我想做的事,不知道这是否是正确的方向.网页:https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/#more-96
以上是我试图扩展 api,但我也决定尝试制作一个自定义端点,看看是否能得到我想要的结果,但到目前为止,我刚刚制作了一个调用但我不知道要做什么的端点写来检索我想要的数据。
自定义端点代码:
function register_custom_route() {
register_rest_route( 'ce/v1', '/bookable',
array(
'methods' => 'GET',
'callback' => 'get_bookable'
)
);
}
function get_bookable( ) {
return array( 'custom' => 'woocommerce here' );
//What code do I write here :(
}
无论如何我可以通过上述方法之一实现我想要的吗? 我对开发人员很陌生,而且我熟悉 javascript 而不是 PHP,因此我需要使用其余的 api,因为我想使用 wordpress/woocommerce 作为无头 cms。
到目前为止,我已经在这个问题Creating WooCommerce Custom API 上展示了壁橱示例
【问题讨论】:
标签: php wordpress woocommerce woocommerce-rest-api