我制作了一个插件,这样我就可以拥有自定义端点。最终,我想提取有关我的可预订产品(woocommerce 预订)的数据。
这是我的插件:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins',
get_option( 'active_plugins' ) ) ) ) {
// Define constants.
define( 'CUSTOM_ENDPOINTS_PLUGIN_VERSION', '1.0.0' );
define( 'CUSTOM_ENDPOINTS_PLUGIN_DIR', __FILE__ );
// Include the main class.
require plugin_dir_path( __FILE__ ) . '/class-rest-custom-woocommerce-endpoints.php';
}
Run Code Online (Sandbox Code Playgroud)
然后在我的主类文件中:
add_action( 'woocommerce_loaded', 'get_data');
add_action( 'rest_api_init', 'custom_endpoint_first');
function custom_endpoint_first(){
register_rest_route( 'cwe/v1/booking', '/get-data',
array(
'methods' => 'GET',
'callback' => 'get_data')
);
}
function get_data() {
$args = array( 'include' => array(28));
$products = wc_get_products( $args );
return $products;
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它返回一个空数组,但当我调用自定义 URL 时它的状态为 …
我想扩展 woocommerce Rest api 以包含其“预订”扩展插件的数据。目前此扩展没有其余 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;
}
Run Code Online (Sandbox Code Playgroud)
当我调用终点时,/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 :(
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以通过上述方法之一实现我想要的吗?我对开发人员很陌生,我熟悉 javascript 而不是 PHP,因此我需要使用其余 …