我做了一个插件,所以我可以有自定义端点。最后,我想拉我的预订产品(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';
}
然后在我的主类文件:
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;
}
我不知道为什么它返回一个空数组,但它具有200层的状态,当我打电话给我的自定义网址。
这条线
'include' => array(28)
意味着你将只能得到一个产品ID为28。 这是否存在产品? 那是你的意图? 如果没有,不妨从这个链接的一些例子 https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query#usage